When I make any property of an interface optional, and while assigning its member to some other variable like this:
interface Person {
name?: string,
age?: string,
gender?: string,
occupation?: string,
}
function getPerson() {
let person = <Person>{name:"John"};
return person;
}
let person: Person = getPerson();
let name1: string = person.name; // <<< Error here
I get an error like the following:
TS2322: Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
How do I get around this error?
The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion.
The error "Argument of type string | undefined is not assignable to parameter of type string" occurs when a possibly undefined value is passed to a function that expects a string . To solve the error, use a type guard to verify the value is a string before passing it to the function.
The typescript compiler performs strict null checks, which means you can't pass a string | undefined variable into a method that expects a string . To fix this you have to perform an explicit check for undefined before calling luminaireReplaceLuminaire() .
TS2322: Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. This happens because TypeScript expects a specific value type but you’re providing an incorrect value type.
To fix the “Type ‘string’ is not assignable to type” error with TypeScript, we should cast the variable to the type we want. export type Fruit = "Orange" | "Apple" | "Banana"; const myString: string = "Banana"; const myFruit: Fruit = myString as Fruit; to create the Fruit literal type.
The name variable is typed as a string, so it only expects to get assigned a value that is a string. TypeScript is basically telling us that the emp.name property might have a value of undefined, which is not compatible with the type of the name variable, which only expects a string.
Type 'null' is not assignable to type 'string'. is because null is a type in TypeScript. You need to type this correctly. I am sorry if this sounds like a rant because it isn't. When I answer questions on stackoverflow I try to teach to give a better understanding of the underlying issues. Three good answers in one hour! Thanks StackOverflow!
You can now use the non-null assertion operator that is here exactly for your use case.
It tells TypeScript that even though something looks like it could be null, it can trust you that it's not:
let name1:string = person.name!;
// ^ note the exclamation mark here
To avoid the compilation error I used
let name1:string = person.name || '';
And then validate the empty string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With