Support Enum Inference In Template Literal (#445)

This commit is contained in:
sinclairzx81
2023-05-25 14:49:16 +09:00
committed by GitHub
parent 479ad6777f
commit 1903324127
7 changed files with 75 additions and 21 deletions
+10 -8
View File
@@ -1,12 +1,14 @@
import { Expect } from './assert'
import { Type, Static } from '@sinclair/typebox'
enum E {
A,
B = 'hello',
C = 42,
{
enum E {
A,
B = 'hello',
C = 42,
}
const T = Type.Enum(E)
Expect(T).ToBe<Static<typeof T>>() // ?
}
const T = Type.Enum(E)
Expect(T).ToBe<Static<typeof T>>() // ?
+28
View File
@@ -42,3 +42,31 @@ import { Type } from '@sinclair/typebox'
const T = Type.TemplateLiteral([Type.Boolean()])
Expect(T).ToInfer<`${boolean}`>()
}
{
// Enum Implicit
enum E {
A,
B,
C,
}
const A = Type.Enum(E)
const T = Type.TemplateLiteral([Type.Literal('hello'), A])
Expect(T).ToInfer<'hello0' | 'hello1' | 'hello2'>()
}
{
// Enum Explicit
enum E {
A,
B = 'B',
C = 'C',
}
const A = Type.Enum(E)
const T = Type.TemplateLiteral([Type.Literal('hello'), A])
Expect(T).ToInfer<'hello0' | 'helloB' | 'helloC'>()
}
{
// Enum Object Explicit
const A = Type.Enum(Object.freeze({ a: 'A', b: 'B' }))
const T = Type.TemplateLiteral([Type.Literal('hello'), A])
Expect(T).ToInfer<'helloA' | 'helloB'>()
}