'Angular 11 any/undefined types warnings

I upgraded my app from Angular v10 to v11 and now I get multiple errors and warnings because some (maybe all) fields can be undefined in a time:

for example:

const savedToken = new Token(JSON.parse(localStorage.getItem(AuthInterceptor.tokenKey)));

must become:

const savedToken = new Token(JSON.parse(<string>localStorage.getItem(AuthInterceptor.tokenKey)));

warning:

Argument of type 'string | null' is not assignable to parameter of type 'string'. 
Type 'null' is not assignable to type 'string' 
// I know the localStorage value can be null in this case.

another example:

public user$: Observable<User>;
// Property 'user$' has no initializer and is not definitely assigned in the constructor.

because the user$ is initialized in ngOnInit() as this.user$ = this.store.pipe()...

How did you solve it?

Even if I deactivate strict from angular.json, the result is same:

"@schematics/angular:application": {
    "strict": false
}

I really need to update all my app code line by line to angular 11? thanks



Solution 1:[1]

in ts.config file you just have to change strict:"true" to strict:"false"

"compilerOptions": { "baseUrl": "./", "outDir": "./dist/out-tsc", "forceConsistentCasingInFileNames": true, "strict": false,

THEN IF IT STILL SHOWS ERROR RESTART VISUAL STUDIO CODE IDE!!!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JoeB