-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new ts 5.0 const generic enum? #367
Comments
@trim21 Hi, thanks for the suggestion :) At this stage, I'm holding off on import { Type, Static } from '@sinclair/typebox'
enum Foo { A, B, C }
const T = Type.Enum(Foo)
function test(value: Static<typeof T>) {
switch(value) {
case Foo.A: return console.log('Foo.A')
case Foo.B: return console.log('Foo.B')
case Foo.C: return console.log('Foo.C')
}
}
test(Foo.A)
test(Foo.B)
test(Foo.C) Btw, Is the explicit expression of Anyway, hope this brings some insights into the future plans for |
It's used in a object as options, so I just write a inline enum. So I don't need to create a ts enum. I'm actually using it as a union, so I don't need to write this: t.Union(
[
t.Literal('s3'),
t.Literal('fs'),
],
{ default: 'fs' },
) |
I mean as opposed to this (using an enum with string values) enum StorageOptionEnum {
s3 = 's3',
fs = 'fs'
}
const StorageOption = Type.Enum(StorageOptionEnum) // const StorageOption = { // produces a union structure
// anyOf: [
// { type: 'string', const: 's3' },
// { type: 'string', const: 'fs' }
// ]
// }
function store(value: Static<typeof StorageOption>) {
switch(value) {
case StorageOption.s3: return console.log('StorageOption.s3')
case StorageOption.fs: return console.log('StorageOption.fs')
}
}
store(StorageOption.s3)
store(StorageOption.fs) Side note, I see that TS 5.0 actually treats all enums as unions now, wondering if TypeBox should yield infer union (as opposed to the original |
Yes I know, I just don't want to write a seprated ts Enum 😆 I write literial value in case. It already yield infer union I think? I try |
Ah, yeah, I guess it would infer a union with // ------------------------------------------------------------------
// Enum Inference
// ------------------------------------------------------------------
type Evaluate<T> = T extends infer O ? { [K in keyof O]: O[K] } : never
type EnumLike = Record<string, string | number>
type EnumStatic<T extends EnumLike> = Evaluate<T[keyof T]>
declare function Enum<const T extends EnumLike>(value: T): EnumStatic<T>
// ------------------------------------------------------------------
// Example
// ------------------------------------------------------------------
enum StorageOptionEnum {
s3 = 's3',
fs = 'fs'
}
const A = Enum(StorageOptionEnum) // A is StorageOptionEnum
const B = Enum({ a: '1', b: '2' }) // B is '1' | '2' It's all good on the TS 5 front, but breaking on versions prior. I'm not sure what the compiler would do if importing declarations using const generics on older versions (maybe it's clever enough to ignore the Thanks against for the suggestion, will close off this one for now as updates are not likely for at least several months, but will revisit this issue at a later time. All the best! |
ts 5.0 new feature const generic type microsoft/TypeScript#51865
could become
without
as const
The text was updated successfully, but these errors were encountered: