Having enabled no-unsafe-any tslint rule in my Angular project I started seeing quite a lot of results. Digging in to them it is flagging properties of parameters injected to constructor as of type any even though they are strongly typed classes. I don't understand why these are being flagged, I'm obviously missing something simple here.
Error message:
Unsafe use of expression of type 'any'.
Example component:
@Component({..)}
export class SampleComponent {
private localString: string;
constructor(private injectedService: SampleService) {
this.localString= this.injectedService.stringProperty;
}
}
And the sample service:
@Injectable({providedIn: 'root'})
export class SampleService {
public stringProperty: string;
}
Note: If the background is of any use this follows on from this question: Typescript not enforcing or checking return type
The problem is probably that a lot of angular decorators don't have actual return types, they return any
. The tslint rules correctly identifies that we are trying to assign any to a place where a decorator is expected.
One solution is to augment the types of the decorators. I activated the rule in one of my projects and these augmentations make the linter happy, you may need to add others as needed:
import { Type } from '@angular/core/src/type';
declare module '@angular/core/src/metadata/directives' {
export interface InputDecorator {
// tslint:disable-next-line:callable-types
(bindingPropertyName?: string): PropertyDecorator;
}
}
declare module '@angular/core/src/di/injectable' {
export interface InjectableDecorator {
(): ClassDecorator;
// tslint:disable-next-line:unified-signatures
(options?: {
providedIn: Type<any> | 'root' | null;
} & InjectableProvider): ClassDecorator;
}
}