A
arktype3mo 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"`
})
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
todo
4 Replies
Dimava
DimavaOP3mo ago
ssalbdivad
ssalbdivad3mo ago
I can see how this could feel nice in some ways, but allowing generics to return things other than types violates a lot of assumptions and feels unintuitive. I think this would be best addressed by implementing a wrapper function that handled this, although admittedly integration with the builtin syntax would be more difficult. It could create scopes automatically based on unions, with the root being all members of the subscope though.
Dimava
DimavaOP3mo ago
Currently I will use an unscoped generic It won't allow using in-scope definitions but will work fine if I only need strings (i.e. a modern TS enum) RootfulModule is technically a "type" enough I guess, but yes it does need consideration
It could create scopes automatically based on unions, with the root being all members of the subscope though
I didn't understand the whole concept, but it's probably weirdly worded thing I'm currently doing
ssalbdivad
ssalbdivad3mo ago
Having actually implemented generics and submodules, I'd say no, it really isn't a type at all. It can just be resolved to one in a scope, but if you pass the actual value somewhere that expects a type it will fail. It might seem like a trivial thing to change, but we'd have to add some kind of maybeUnwrap to every API that accepts a type instance currently. As is, it's not something I would at all consider. Maybe some other approach to solving the same problem could be provided as an internal API, but I'm currently not open to the idea of returning modules or anything else from generics
Want results from more Discord servers?
Add your server