You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// in 2.1.4 iShouldBeStringLiteral is now a stringletiShouldBeStringLiteral=type('This is a string')exportfunctiontype<T>(label: T|''): T{return<T>label;}
Expected behavior:
Before updating to typescript 2.1.4 the function transformed string to string literals with the same value Actual behavior:
Now, even though the function returns a string literal type, assigning the return value to a variable / object property, will downcast it to string
In the second image, I would expect x to be of type 'String literal'
This is especially in redux like scenarions, where you have to switch based on a constant and need the exhaustive checking in order to get type safety inside every case branch
edit: I know the solution is a bit hackish, but it helps a lot. If there is any other way of doing this, I'm more than open to suggestions
The text was updated successfully, but these errors were encountered:
Also, the non templated version the function works as intended
function type(val: any){
return <'this is string literal'>val;
}
// stringLiteral appears as ' let stringLiteral: "this is string literal" '
let stringLiteral = type('this is string literal');
In 2.1 we preserve literal types of expressions and variables for a long as possible, but when we infer a literal type for a mutable location such as a let or var variable, we widen to the base primitive type. However, if you declare iShouldBeStringLiteral using const you shouldn't need the type function at all.
TypeScript Version: 2.1.4
Code
Expected behavior:
Before updating to typescript 2.1.4 the function transformed string to string literals with the same value
Actual behavior:
Now, even though the function returns a string literal type, assigning the return value to a variable / object property, will downcast it to string
In the second image, I would expect
x
to be of type'String literal'
This is especially in redux like scenarions, where you have to switch based on a constant and need the exhaustive checking in order to get type safety inside every case branch
edit: I know the solution is a bit hackish, but it helps a lot. If there is any other way of doing this, I'm more than open to suggestions
The text was updated successfully, but these errors were encountered: