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";'