I am trying to use a signal value that can be undefined
, but I am stumped on how to tell the linter that the value is defined after I check that it actually is defined.
Example
export class MyComponent {
mySignal$: WritableSignal<{ msg: string } | undefined> = signal(undefined);
constructor() {
this.mySignal$.set({ msg: 'Hello there!' });
effect(() => {
if(this.mySignal$()) {
console.log(this.mySignal$().msg) // Object is possibly 'undefined'.ts(2532)
// Works but feels like there should be an easier way
// console.log((this.mySignal$() as { msg: string }).msg)
}
});
}
}
Relevant Installed Packages
"@angular-eslint/eslint-plugin": "16.0.3"
"@angular-eslint/eslint-plugin-template": "16.0.3"
"@angular-eslint/template-parser": "16.0.3"
"@nx/eslint-plugin": "16.8.1"
"@typescript-eslint/eslint-plugin": "5.60.1"
"@typescript-eslint/parser": "5.60.1"
"eslint": "8.46.0"