arktypea
arktype10mo ago
Untlsn

Match `.in` with loose type

In docs there is example
type Data =
  | {
    id:       1
    oneValue: number
  }
  | {
    id:       2
    twoValue: string
  };
const discriminateValue = type.match
// .in allows you to specify the input TypeScript allows for your matcher
  .in<Data>()
// .at allows you to specify a key at which your input will be matched
  .at('id')
  .match({
    1:       (o) => `${o.oneValue}!`,
    2:       (o) => o.twoValue.length,
    default: 'assert',
  });


Its work good because match know how rest of the type will look like, so we have acess to oneValue or twoValue

But when i change it to:
type Data = {
  id:    '1' | '2'
  value: number
};
const discriminateValue = type.match
// .in allows you to specify the input TypeScript allows for your matcher
  .in<Data>()
// .at allows you to specify a key at which your input will be matched
  .at('id')
  .match({
    1:       (o) => `${o.value}!`,
    2:       (o) => o.value.length,
    default: 'assert',
  });


Typescript say that only id exist

Is this an issue, or intentional?

Probably second one, because i need to make
typeof o & Omit<Data, keyof typeof o>
to get anything else then never from typescript
Then what should I use as alternative?
Was this page helpful?