nvie / decoders

Elegant validation library for type-safe input data (for TypeScript and Flow)
https://decoders.cc
MIT License
357 stars 27 forks source link

taggedUnion not working correctly #1127

Closed programever closed 2 months ago

programever commented 2 months ago

Description of the bug My tsconfig.json has strict: true, I have a union type of tag A | B | C, I expect TSC error for decoder of invalid tag D

Expected behavior taggedUnion decoder for invalid tag should be TSC error

To Reproduce

type Should = { _t: "Yes" } | { _t: "No" } | { _t: "Maybe" }
const shouldDecoder: JD.Decoder<Should> = JD.taggedUnion("_t", {
  New: JD.object({ _t: JD.constant("New") }), // SHOULD BE ERROR
  Yes: JD.object({ _t: JD.constant("Yes") }),
  No: JD.object({ _t: JD.constant("No") }),
  Maybe: JD.object({ _t: JD.constant("Maybe") }),
})

Temporary solution Use either OR Downgrade to 2.1.0

type Should = { _t: "Yes" } | { _t: "No" } | { _t: "Maybe" }
const tempDecoder: JD.Decoder<Should> = JD.either(
  JD.object({ _t: JD.constant("New") }), // TSC ERROR
  JD.object({ _t: JD.constant("Yes") }),
  JD.object({ _t: JD.constant("No") }),
  JD.object({ _t: JD.constant("Maybe") }),
)

Additional context Decoders version: 2.4.0 TypeScript version: 5.3.3 Flow version: Node version: 20.12.2

nvie commented 2 months ago

Hey @programever, thanks for reporting! This is indeed a regression. I've fixed it in #1130. Could you run the following command locally to upgrade?

$ npm i https://pkg.pr.new/decoders@1130

If you can confirm this fixes your issue, I'll formally release this in 2.4.1 — thank you! 🙏

programever commented 2 months ago

Hi @nvie,

Thank you very much for quick fix! All are working well at my end! 🙏

nvie commented 2 months ago

This is now published as v2.4.2.