arktypea
arktype15mo ago
Dimava

Generic modules, Enums and AnyOf

There are at least 3 threads asking for OneOf
I suggest making a Enum generic (called OneOf maybe) that works as following:
  • it accepts an object definition O
  • it has a root type which allows any of the property values O[keyof O]
  • it is a module with all property types as its values, Module<{ [K in keyof O]: Type<O[K]> }>
    Then it's easy to use it for both cases of an enum with constant values
    scope({
     _e: { One: 'one', Two: 'two', Three: 'three' }.
     oneTwoThree: 'Enum<_e>',
     list: [{ num: 'oneTwoThree' },'[]'],
     first: { num: 'oneTwoThree.one' },
     second: { num: 'oneTwoThree.tow' }, // type error, this typo be easily missed if it was `"tow"`
    })

    and when you just want to intersect a bunch of types
    todo
Was this page helpful?