Skip to content
Discussion options

You must be logged in to vote

For discriminated unions like this, the discriminator (in this case "valid") needs to be a literal (like "adsf" or true, not string or boolean. You need to tell TypeScript to treat literal values as literals. In this case just add as const to the boolean literals like this, and it works as expected:

  if (parseResult.success) {
    return { data: parseResult.data, valid: true as const, setData }; // data is recognized as `S` type -> correct!
  } else {
    return { data: data, valid: false as const, setData }; // data is recognized as `I` type -> correct!
  }

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@mzalevski
Comment options

@mzalevski
Comment options

@colinhacks
Comment options

Answer selected by mzalevski
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants