Skip to main content

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