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
Add support for defining: string - 'a' (string minus string literal type) #12182
The proposed narrowing of Z wouldn't be correct because a { type: string, b: string } could very well have a type property with the value 'a'. The only way it would be correct is if you could somehow express a type string - 'a' (i.e. a string with any value but 'a'), but we don't have that capability.
BTW, #11587 is about narrowing string to a literal type, not about narrowing something like 'a' | string. In general it is not meaningful to have types like 'a' | string because it is really the same as just string and the compiler will reduce it to that the minute it performs subtype reduction.
changed the title [-]Narrow string and number types in literal equality checks for nested objects[/-][+]Add support for defining: string - 'a' (string minus string literal type)[/+]on Nov 12, 2016
I wonder if we could say that 'a' should take precedence over string in case of Z, but if not then
string - 'a' would solve our use case - so I renamed that title to reflect this feature - but if you feel it's Out of scope - feel free to close it.
We are trying to migrate Redux actions/reducers to TS and such a catch-all action would be very useful, for example:
interfaceAddToDo{type: 'addToDo'data: {name: stringcompleted: boolean}}// ... - other actionsinterfaceCatchAll{type: string// `string - 'addToDo'` would help heredata: any}typeActions=AddToDo|CatchAllfunctionreduce(action: Actions){switch(action.type){case'addToDo':
action// type: Actions -> ideally: AddToDobreak;/// any other actions that don't have any inteface definedcase'someOtherAction':
action// type: Actions -> ideally: CatchAll, so that action.data is anybreak;case'someOtherAction2':
action// type: Actions -> ideally: CatchAll, so that action.data is anybreak;// ... more actions}}
@wallverb That is somewhat strange because the "any other actions" case doesn't do what it says. It only handles values that are tagged with 'someOtherAction'. If all other values are always so tagged, then using that tag as the literal type of CatchAll.type correctly describes the behavior of reduce.
Aluan Haddad (@aluanhaddad) Sorry if my example was not clear enough - I updated. The thing is that there are hundreds of such redux actions that don't have any type defined for them and only handful of actions (like AddToDo) that do have them.
Having something like this:
Activity
ahejlsberg commentedon Nov 11, 2016
The proposed narrowing of
Zwouldn't be correct because a{ type: string, b: string }could very well have atypeproperty with the value'a'. The only way it would be correct is if you could somehow express a typestring - 'a'(i.e. a string with any value but'a'), but we don't have that capability.BTW, #11587 is about narrowing
stringto a literal type, not about narrowing something like'a' | string. In general it is not meaningful to have types like'a' | stringbecause it is really the same as juststringand the compiler will reduce it to that the minute it performs subtype reduction.[-]Narrow string and number types in literal equality checks for nested objects[/-][+]Add support for defining: string - 'a' (string minus string literal type)[/+]normalser commentedon Nov 12, 2016
Anders Hejlsberg (@ahejlsberg) Thanks for the response.
I wonder if we could say that
'a'should take precedence overstringin case ofZ, but if not thenstring - 'a'would solve our use case - so I renamed that title to reflect this feature - but if you feel it's Out of scope - feel free to close it.We are trying to migrate Redux actions/reducers to
TSand such acatch-allaction would be very useful, for example:Thanks
aluanhaddad commentedon Nov 12, 2016
@wallverb That is somewhat strange because the "any other actions" case doesn't do what it says. It only handles values that are tagged with
'someOtherAction'. If all other values are always so tagged, then using that tag as the literal type ofCatchAll.typecorrectly describes the behavior ofreduce.normalser commentedon Nov 12, 2016
Aluan Haddad (@aluanhaddad) Sorry if my example was not clear enough - I updated. The thing is that there are hundreds of such redux actions that don't have any type defined for them and only handful of actions (like AddToDo) that do have them.
Having something like this:
would allow to achieve that (to narrow to
CatchAll), but I understand if that would be Out of Scope / no interest by anyone elsealuanhaddad commentedon Nov 12, 2016
@wallverb What I mean is that you need to handle that in the
defaultclause. If they actually have no union tag at runtime, you can writeand type narrowing will actually work with
case(if you are using--strictNullChecksthenundefinedis incompatible withstring).mhegazy commentedon Nov 14, 2016
seems like a duplicate of #7993