Skip to content

Typescript: Type of Action Parameter in Union #2150

Discussion options

You must be logged in to vote

Since I've been picking up some of the TS work, I can pitch in.

Both MST and TS are doing the right thing. In updateItemPayload, item.updatePayload is going to be of this type:

type F = ((payload: boolean[][]) => void) | ((payload: string) => void)

It doesn't know which of those two the function is, so what happens if we give F a string and the underlying instance is actually the function that takes a boolean[][]? That's why you're seeing boolean[][] & string, because if the payload you're providing is both possible types at the same time, it'll work for all members of the union. A simplified version of this in the TS playground:

Given your example, you'd unfortunately have to narrow bot…

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by coolsoftwaretyler
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
help/PR welcome Help/Pull request from contributors to fix the issue is welcome Typescript Issue related to Typescript typings
3 participants
Converted from issue

This discussion was converted from issue #1371 on February 24, 2024 23:11.