-
Notifications
You must be signed in to change notification settings - Fork 12.8k
make the strict equality comparison operator a typeguard for string literal types #7447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
👍 |
I had a draft of this kind of guard working in #6062, and @ahejlsberg has added some form of this guard to #7140 for at least |
+1 |
As a note, I don't think we could narrow from |
The problem here is this breaking change (and arguably undesirable behavior): const hey: 'hey' = 'hey';
const value = Math.random() > 0.5 ? 'hey' : 'nay';
if (value === hey) {
// here expected value to be of type 'hey' (we just asserted it), actual string
let q = value;
if (...) {
q = 'never mind'; // error, can't assign "never mind" to "hey"
}
} The use case for changing this isn't super compelling since you have the right operand |
mind me asking, can const hey: 'hey' = 'hey';
const value = Math.random() > 0.5 ? 'hey' : 'nay';
if (value === hey) {
let q = value; // originaly string but then asserted but then might be reassigned
if (...) {
q = 'never mind'; // no pasa nada
}
} |
How is this breaking change different from other breaking changes related to narrowing? They all can cause that narrowing causes that the type of a variable is too specific. In this case it's very easy to solve it, just add |
This was just suggested again in #9314. It is related to @ivogabe's suggestion here and I'm starting to think it has some merit. Specifically, for any type guard on a reference |
To be clear, my concerns about narrowing were about narrowing from |
Agreed, we only want to narrow from unions of string literal types, not from the |
The original suggestion in this issue along with a number of other features is now implemented by #9407. |
1.9.0-dev.20160217
Code
The text was updated successfully, but these errors were encountered: