Aller au contenu principal

generateOptimizedPrimitiveUnion()

generateOptimizedPrimitiveUnion(varName, branches, ctx, errorMessage?): object

Experimental

Generates optimized union check for primitive types using typeof.


Parameters

varName: string

The variable name to check

branches: UnionBranchMeta[]

Union branches (must all have typeofCheck)

ctx: GeneratorContext

The generator context

errorMessage?: string

Custom error message


Returns: object

Generated code and updated context

code: string[]

ctx: GeneratorContext


Since

2.0.0


Example

// For union([string(), number()])
const result = generateOptimizedPrimitiveUnion("value", branches, ctx);
// result.code = 'if (typeof value !== "string" && typeof value !== "number") return "Expected string or number";'