@@ -31,13 +31,15 @@ THE SOFTWARE.
3131import { TUnreachable , Unreachable } from '../../../system/unreachable/index.ts'
3232import { Guard } from '../../../guard/index.ts'
3333
34+ import { type TLiteral , type TLiteralValue , Literal , IsLiteral } from '../../types/literal.ts'
3435import { type TSchema , IsSchema } from '../../types/schema.ts'
35- import { type TUnion , Union , IsUnion } from '../../types/union.ts'
3636import { type TString , String } from '../../types/string.ts'
37- import { type TLiteral , type TLiteralValue , Literal , IsLiteral } from '../../types/literal.ts'
37+ import { type TTemplateLiteral } from '../../types/template-literal.ts'
38+ import { type TUnion , Union , IsUnion } from '../../types/union.ts'
3839
3940import { type TParsePatternIntoTypes , ParsePatternIntoTypes } from '../patterns/pattern.ts'
4041import { type TTemplateLiteralFinite , TemplateLiteralFinite } from './finite.ts'
42+ import { TemplateLiteralCreate } from './create.ts'
4143
4244// ------------------------------------------------------------------
4345// FromLiteral
@@ -176,19 +178,21 @@ function DecodeTypes<Types extends TSchema[]>(types: [...Types]): TDecodeTypes<T
176178/** Decodes a TemplateLiteral into a Type. If the TemplateLiteral yields a non-finite set, the return value is TString */
177179export type TTemplateLiteralDecode < Pattern extends string ,
178180 Types extends TSchema [ ] = TParsePatternIntoTypes < Pattern > ,
179- Finite extends boolean = TTemplateLiteralFinite < Types > ,
180181 Result extends TSchema = (
181- Finite extends true
182- ? TDecodeTypes < Types >
183- : TString
182+ Types extends [ ] // Failed to Parse
183+ ? TString
184+ : TTemplateLiteralFinite < Types > extends true
185+ ? TDecodeTypes < Types >
186+ : TTemplateLiteral < Pattern >
184187 )
185188> = Result
186189/** Decodes a TemplateLiteral into a Type. If the TemplateLiteral yields a non-finite set, the return value is TString */
187190export function TemplateLiteralDecode < Pattern extends string > ( pattern : Pattern ) : TTemplateLiteralDecode < Pattern > {
188191 const types = ParsePatternIntoTypes ( pattern )
189- const finite = TemplateLiteralFinite ( types )
190- const result = finite
191- ? DecodeTypes ( types )
192- : String ( )
192+ const result = Guard . IsEqual ( types . length , 0 ) // Failed to Parse
193+ ? String ( ) // ... Pattern cannot be typed, so discard
194+ : TemplateLiteralFinite ( types )
195+ ? DecodeTypes ( types )
196+ : TemplateLiteralCreate ( pattern )
193197 return result as never
194198}
0 commit comments