0

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?

1
  • 1
    Closing the question since this is a common TS error. Please try one of the solutions provided in the linked post. Commented Jan 26, 2022 at 15:11

1 Answer 1

1

This error shows up because the value can be undefined.

Couple ways to fix it:

  1. Give it a default value
  2. Initialize the value in the constructor of the class it uses it
  3. 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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.