'I use Angular13 and I want to resolve the variable errors [duplicate]
I want to resolve this error
Type 'MyInterface | undefined' is not assignable to type 'MyInterface '. Type undefined' is not assignable to type 'MyInterface '.ts(2322)
This is what I want to do.
right: MyInterface ;
this.right=this.responseData.filter( condition );
and for resolving this I should use my variable like this
right: MyInterface | any;
but I don't want to use any! Any other ways to resolve this?
Solution 1:[1]
This error shows up because the value can be undefined.
Couple ways to fix it:
- Give it a default value
- Initialize the value in the constructor of the class it uses it
- Change the type of the field to MyClass | undefined, as the error shows.
The point of having strict type checking is to avoid having bugs related to stuff like this, so giving your field the type "undefined" if it can be undefined, is a good practice for people using the field as they know it can be undefined.
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 | TanguyB |
