close

[Fixed] Typescript Type ‘string’ is not assignable to type

Today We are Going To Solve Typescript Type ‘string’ is not assignable to type in javascript. Here we will Discuss All Possible Solutions and How this error Occurs So let’s get started with this Article.

How to Fix Typescript Type ‘string’ is not assignable to type Error?

  1. How to Fix Typescript Type ‘string’ is not assignable to type Error?

    To Fix Typescript Type ‘string’ is not assignable to type Error just Use const. You have to just type your string with const assertion. Like given below: let food = 'pizza' as const; or let food = <const> 'pizza'; Thus by using const it will not turn into a string. So just use const in your code. It will help you. Thank you!

  2. Typescript Type ‘string’ is not assignable to type

    To Fix Typescript Type ‘string’ is not assignable to type Error just run this commands. Try this. By using const you can sort your error. export type Food = "pizaa" | "burger" | "dhosa"; let myString: string = "burger"; let myFood: Food = myString as Food; let food = "burger" as const; I hope this will help you.

Solution 1 : Use const

You have to just type your string with const assertion. Like given below:

let food = 'pizza' as const; 
or
let food = <const> 'pizza';

Thus by using const it will not turn into a string. So just use const in your code. It will help you. Thank you!

Solution 2 : run this commands

Try this. By using const you can sort your error.

export type Food = "pizaa" | "burger" | "dhosa";
let myString: string = "burger";

let myFood: Food = myString as Food;
let food = "burger" as const;

I hope this will help you.

Conclusion

So these were all possible solutions to this error. I hope your error has been solved by this article. In the comments, tell us which solution worked? If you liked our article, please share it on your social media and comment on your suggestions. Thank you.

Also Read This Solutions

Leave a Comment