-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Tagged union common property types are not narrowed #10976
Comments
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 function 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. |
This would be really nice to have. I find myself often writing type guards inside const 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)
) |
TypeScript Version: 2.0.2-rc
Code
Expected behavior:
Type of payload argument passed to
move
function should beMovePayload
Actual behavior:
TS2345: Argument of type 'MovePayload | ChatPayload' is not assignable to parameter of type 'MovePayload'
Of course, doing
move(payload as MovePayload)
works correctly.The text was updated successfully, but these errors were encountered: