Repository navigation
Tagged union common property types are not narrowed #10976
Description
Activity
- changed the title
[-]Tagged union common properties are not narrowed[/-][+]Tagged union common property types are not narrowed[/+]on Sep 18, 2016 Actually.. this is user error. I believe the issue lies with doing the destructure in the reducer, i.e.
function reducer({ kind, payload }: Action)
Because I haven't switched on the
kindat this point, flow control analysis cannot narrow down the type ofpayload. The following works finefunction reducer(action: Action) { switch (action.kind) { case "move": return move(action.payload) } }
I'm wondering if there is a way to make destructuring on tagged unions a bit more ergonomic? It seems like something that would be a common pattern, and having to duplicate destructures in each case could be cumbersome.
once an object is destructed, the compiler can no longer make any assumptions about the relationships between the parts. Doing so requires data-flow analysis and alias tracking which is not trivial tasks.
- addedWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
on Sep 20, 2016 felixfbecker commented
on May 2, 2018 ContributorMore actionsThis would be really nice to have. I find myself often writing type guards inside
filter()callbacksconst elements = new Observable<HTMLElement | null>() elements.pipe( filter((element): element is NonNullable<typeof element> => !!element) )
which is painful if you just want to narrow the type of one property/tuple element, because you need to repeat the type of the other members and cannot use destructuring:
const mouseOvers = new Observable<MouseEvent>() mouseOvers.pipe( withLatestFrom(elements), filter((data): data is [typeof data[0], NonNullable<typeof data[1]>] => !!data[1]) )
vs
mouseOvers.pipe( withLatestFrom(elements), filter(([, element]): element is NonNullable<typeof element> => !!element) )
- locked and limited conversation to collaborators
on Jul 31, 2018
TypeScript Version: 2.0.2-rc
Code
Expected behavior:
Type of payload argument passed to
movefunction should beMovePayloadActual behavior:
TS2345: Argument of type 'MovePayload | ChatPayload' is not assignable to parameter of type 'MovePayload'Of course, doing
move(payload as MovePayload)works correctly.