Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: Type 'string | undefined' is not assignable to type 'string'

Tags:

typescript

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?

like image 493
asdasd Avatar asked Oct 12 '22 06:10

asdasd


People also ask

Is not assignable to type string Ts?

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.

Is not assignable to parameter of type string?

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.

How do you assign a string undefined to a string in TypeScript?

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() .

Why is type undefined not assignable to type string?

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.

How to fix type ‘string’ is not assignable to type with typescript?

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.

Why is my name variable returning undefined in typescript?

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.

Why can't I type null type string in typescript?

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!


2 Answers

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  
like image 683
yannick1976 Avatar answered Oct 13 '22 18:10

yannick1976


To avoid the compilation error I used

let name1:string = person.name || '';

And then validate the empty string.

like image 170
AndreFontaine Avatar answered Oct 13 '22 18:10

AndreFontaine