---
title: "KeyOf: type in Types, TypeShade API reference for developers"
description: "KeyOf, a type in Types. Turns a ShaderType literal into the precise string key that Node carries as its compile-time phantom…"
url: https://typeshade.dev/api/keyof/
language: en
---

# `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'`
`import type { KeyOf } from 'typeshade/core/ir'`

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

## Syntax

```ts
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`](https://typeshade.dev/api/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`](https://typeshade.dev/reference/errors/sd0002/) check in [`typeKey`](https://typeshade.dev/api/typekey/) and [`typeEq`](https://typeshade.dev/api/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`](https://typeshade.dev/api/structt/) or [`arrayT`](https://typeshade.dev/api/arrayt/) result); a widened `ShaderType` collapses to the `string` fallback. Every arm mirrors one `case` of [`typeKey`](https://typeshade.dev/api/typekey/), and a new `ShaderType` variant needs an arm in both.

## See also

- [`Node`](https://typeshade.dev/api/node/)
- [`typeKey`](https://typeshade.dev/api/typekey/)
- [`typeEq`](https://typeshade.dev/api/typeeq/)
- [`structT`](https://typeshade.dev/api/structt/)
- [`arrayT`](https://typeshade.dev/api/arrayt/)

## Source

[`src/core/ir/types/key-of.ts`, line 13](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/ir/types/key-of.ts#L13), at commit af54ddad5
