KeyOf
On this page

KeyOf

Type in Types

Turns a ShaderType literal into the precise string key that Node carries as its compile-time phantom: KeyOf<typeof vec3fT> resolves to 'vec3<f32>', so a vec2 and vec3 operand mismatch fails tsc before the runtime SD0002 check in typeKey and typeEq would catch it.

import type { KeyOf } from 'typeshade'

The signature, the description and the examples come from the compiler's own source at commit 26de7be8.

Syntax

type KeyOf<T> = T extends { kind: 'scalar'; scalar: infer S extends string }
? S
: T extends { kind: 'f64' }
? 'f64'
: T extends { kind: 'vec64'; n: infer N extends number }
? `vec${N}<f64>`
: T extends { kind: 'vec'; n: infer N extends number; elem: infer E extends string }
? `vec${N}<${E}>`
: T extends {
kind: 'mat';
cols: infer C extends number;
rows: infer R extends number;
elem: infer E extends string;
}
? `mat${C}x${R}<${E}>`
:
T extends { kind: 'struct'; name: infer N extends string }
? `struct:${N}`
: T extends { kind: 'array'; elem: infer E; size: infer S }
? S extends number
? `array<${KeyOf<E>},${S}>`
: `array<${KeyOf<E>}>`
: T extends { kind: 'atomic'; elem: infer E extends string }
? `atomic<${E}>`
: T extends { kind: 'void' }
? 'void'
:
T extends { kind: 'texture'; dim: '2d-ms'; elem: infer E extends string }
? `texture_multisampled_2d<${E}>`
:
T extends { kind: 'texture'; dim: '2d-array'; elem: infer E extends string }
? `texture_2d_array<${E}>`
: T extends { kind: 'texture'; dim: '2d'; elem: infer E extends string }
? `texture_2d<${E}>`
:
T extends { kind: 'texture'; dim: 'cube'; elem: infer E extends string }
? `texture_cube<${E}>`
: T extends { kind: 'texture'; dim: '3d'; elem: infer E extends string }
? `texture_3d<${E}>`
: T extends { kind: 'texture'; dim: '1d'; elem: infer E extends string }
? `texture_1d<${E}>`
: T extends {
kind: 'texture';
dim: 'cube-array';
elem: infer E extends string;
}
? `texture_cube_array<${E}>`
: T extends { kind: 'sampler' }
? 'sampler'
: string;

Description

Turns a ShaderType literal into the precise string key that Node carries as its compile-time phantom: KeyOf<typeof vec3fT> resolves to 'vec3<f32>', so a vec2 and vec3 operand mismatch fails tsc before the runtime SD0002 check in typeKey and typeEq would catch it. It narrows only against a T that kept its literal shape (the as const satisfies ShaderType constants in this module, or a structT or arrayT result); a widened ShaderType collapses to the string fallback. Every arm mirrors one case of typeKey, and a new ShaderType variant needs an arm in both.

See also

Source

src/core/ir/types.ts, line 832, at commit 26de7be8

Edit this page Report a problem