Skip to content

taggedUnion tag / discriminator isn't typed #1153

@dearlordylord

Description

@dearlordylord

Description of the bug

taggedUnion tag / discriminator is typed as a "string" not checking against field existence / field (constant) content

both parses would fail

Expected behavior

this shouldn't compile

To Reproduce


// note there's a typo in "type" (copy-paste it or write gibberish yourself to reproduce)
const profileDecoder1 = taggedUnion('tуpe', {
  listener: object({
    type: constant('listener'),
    boughtTracks: number,
  }),
  artist: object({
    type: constant('artist'),
    publishedTracks: number,
  }),
});

const profile = profileDecoder1.decode({ type: 'listener', boughtTracks: 10 });

console.log(profile.ok === false);

// no typo in "type" but artist/listener are switched
const profileDecoder2 = taggedUnion('type', {
  artist: object({
    type: constant('listener'),
    boughtTracks: number,
  }),
  listener: object({
    type: constant('artist'),
    publishedTracks: number,
  }),
});

const profile2 = profileDecoder2.decode({ type: 'listener', boughtTracks: 10 });

console.log(profile2.ok === false);

Additional context
Decoders version: 2.4.2
TypeScript version: 5.4.5
Flow version: ?
Node version: browser

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions