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
{{ message }}
Repository navigation
Literal narrowing lost without explicit const assertion #39763
consta1='A1';// as constconsta2='A2';// as constconstactionCreators={a1: ()=>({type: a1}),a2: (b)=>({type: a2, b }),};exportdefault(state={},action)=>{switch(action.type){casea2:
returnObject.assign(Object.assign({},state),{b: action.b});default:
returnstate;}};
I see... Thanks for the explanation provided in the linked PRs - but my intuition was built from IDEs type hint tooltips, neither VS Code nor IntelliJ distinguish between the 2 different types (widening and non-widening literals both look the same):
I wanted to ask where it's documented, but now that I know what to look for, I found it in mentioned in cost assertions section:
no literal types in that expression should be widened (e.g. no going from "hello" to string)
The feature was very hard to discover for me (and surprising when I did), but I understand how it makes sense I guess, so I will either get used to it or wait for a new readonly-by-default mode proposed in #32758.
my intuition was built from IDEs type hint tooltips, neither VS Code nor IntelliJ distinguish between the 2 different types (widening and non-widening literals both look the same)
Yeah, we actually discussed displaying something to distinguish between the two, but we decided against the additional noise as in most cases it doesn't matter.
Activity
ahejlsberg commentedon Jul 27, 2020
This is working as intended. See #11126, and also #29510.
Aprillion commentedon Jul 28, 2020
I see... Thanks for the explanation provided in the linked PRs - but my intuition was built from IDEs type hint tooltips, neither VS Code nor IntelliJ distinguish between the 2 different types (widening and non-widening literals both look the same):
I wanted to ask where it's documented, but now that I know what to look for, I found it in mentioned in cost assertions section:
The feature was very hard to discover for me (and surprising when I did), but I understand how it makes sense I guess, so I will either get used to it or wait for a new readonly-by-default mode proposed in #32758.
ahejlsberg commentedon Jul 28, 2020
Yeah, we actually discussed displaying something to distinguish between the two, but we decided against the additional noise as in most cases it doesn't matter.