Repository navigation
make the strict equality comparison operator a typeguard for string literal types #7447
Description
Activity
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptIn DiscussionNot yet reached consensusNot yet reached consensus
on Mar 9, 2016 aluanhaddad commented
on Mar 11, 2016 ContributorMore actions👍
I had a draft of this kind of guard working in #6062, and Anders Hejlsberg (@ahejlsberg) has added some form of this guard to #7140 for at least
nullandundefinedvalue types, for reference.+1
- addedDomain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedUnit types including string literal types, numeric literal types, Boolean literals, null, undefined
on Mar 17, 2016 DanielRosenwasser commented
on Mar 17, 2016 MemberMore actionsAs a note, I don't think we could narrow from
stringbecause that would cause breaking changes.Reacted by Herrington Darkholme- addedAwaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureand removedIn DiscussionNot yet reached consensusNot yet reached consensus
on Jun 9, 2016 RyanCavanaugh commented
on Jun 9, 2016 MemberMore actionsThe 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
heyof===available to you for use inside theifbody already with the type you want already, so narrowing the left operand doesn't seem super useful. Obviously one or the other may have been computed and not stored, but it's still probably not worth taking a breaking change for unless we can see how it's going to make some other piece of code a lot better.zpdDG4gta8XKpMCd commented
on Jun 9, 2016 AuthorMore actionsmind me asking, can
qbestring | 'hey'thank to flow analysis?
'hey'in one context andstringin another?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
: string.This was just suggested again in #9314. It is related to Ivo Gabe de Wolff (@ivogabe)'s suggestion here and I'm starting to think it has some merit. Specifically, for any type guard on a reference
x.y, we would also narrowxitself based on the narrowed type ofx.y. It effectively generalizes discriminant type guards to work over the entire set of existing type guards.DanielRosenwasser commented
on Jun 22, 2016 MemberMore actionsTo be clear, my concerns about narrowing were about narrowing from
string. If you had a union of string types, I'd argue that you are probably using the strings as tags and want the narrowing behavior for things like overloads and ensuring exhaustiveness. On the other hand, narrowing fromstringmore likely to break transitive assignments, and there is no such thing as exhaustive checking of allstrings.Agreed, we only want to narrow from unions of string literal types, not from the
stringtype itself.The original suggestion in this issue along with a number of other features is now implemented by #9407.
- addedFixedA PR has been merged for this issueA PR has been merged for this issueand removedAwaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this feature
on Aug 3, 2017 - locked and limited conversation to collaborators
on Jun 19, 2018
1.9.0-dev.20160217
Code