When we try to assign a string to a variable of custom type, then i am facing Type ‘string’ is not assignable to type this error.
In this article, we will discuss about this Type ‘string’ is not assignable to type error in Java. And how to resolve the error all the possible solutions with examples.
How to Type ‘string’ is not assignable to type Occurs?
This is a common error to assign a string to a variable of custom type you might face while running a Java application. It looks as like below:
Type 'string' is not assignable to type
How To Fix Type ‘string’ is not assignable to type Error?
There are different ways to fix Type ‘string’ is not assignable to type this error. Let us take a look at every solution.
- How To Solve Type ‘string’ is not assignable to type Error?
To Solve Type ‘string’ is not assignable to type Error From the new ‘const‘ assertion So You have to Use const assertion Here is my example: let bike = “orange” as const; OR let bike = ‘orange’. Now, Your error must be solved.
- Type ‘string’ is not assignable to type
To Solve Type ‘string’ is not assignable to type Error From the new ‘const‘ assertion So You have to Use const assertion Here is my example: let bike = “orange” as const; OR let bike = ‘orange’. Now, Your error must be solved.
Solution 1: Use const
assertion
Since Typescript 3.4 introduced the new ‘const‘ assertion So You have to Use const assertion Here is my example.
let fruit = 'orange' as const; // or...
let fruit = <const> 'orange';
Now, Your error will be solved.
Conclusion
In this article, we have discussed what causes the error and we have discussed ways to fix the error.
we hope this article has been informative. Thank you for reading. Kindly comment and let us know if you found it helpful.