---
title: "Rule 4.1, Types: a TypeShade language design rule"
description: "Rule 4.1 of the TypeShade language design, in Types. Every type an author writes must be a type borrowed from WGSL under a TypeScript spelling…"
url: https://typeshade.dev/reference/rules/4-1/
language: en
---

# Rule 4.1

Chapter 4, Types Test

Every type an author writes must be a type borrowed from WGSL under a TypeScript spelling, a member of the `f64` family ([Rule 4.4](https://typeshade.dev/reference/rules/4-4/)), a key type, or a type ECMAScript defines that the compiler represents ([Rule 1.5](https://typeshade.dev/reference/rules/1-5/)). A union of string literal types, or one string literal type, is a key type (change 0081): a `u32` under that spelling, whose members are its values, each held as its id, the member’s index in the order of the members’ UTF-16 code units. A key type takes `===`, `!==` and `switch` over its members, and no other operator; a value of another key type, or a number, is no value of it. `Record<K, V>` with `K` a key type is an `array<V, N>`, `N` the number of members, indexed by the member’s id (change 0081): `r[k]` with `k` of the key type, `r.g` and `r["g"]` the constant index of `"g"`, and an object literal with one value for each member, in any order, its value.

`Record<K, V>` also takes an enum or a union of integer literals when its distinct values are exactly 0 through N-1, indexed by the member value; other numeric domains are refused with their actual values (change 0081, decision 2).

The rule text and the parts under it are the compiler's own English, as the design document writes them.

## Rationale

the layout, the constructors, and the builtins of a type borrowed from WGSL are WGSL’s, and a type WGSL does not have has none of those; a type ECMAScript defines keeps ECMAScript’s meaning and takes the representation the compiler chooses ([Rule 1.5](https://typeshade.dev/reference/rules/1-5/)), as `Map` and `Set` do (change 0080).

## Derives from

`changes/0100-typescript-meaning.md` (section 2); [Types](https://gpuweb.github.io/gpuweb/wgsl/#types), [Plain Types](https://gpuweb.github.io/gpuweb/wgsl/#plain-types-section) and [Texture and Sampler Types](https://gpuweb.github.io/gpuweb/wgsl/#texture-sampler-types).

## How it is verified

Checked by a test. A test, a gate script or a CI workflow names this rule, and the traceability check fails when a file listed below stops naming it.

Where the rule says the compiler enforces it:

`SUPPORTED_TYPE_NAMES` in `src/compiler/ts/type-map.ts` (`TS8002 UNKNOWN_TYPE` names the whole list) and `surface-names.test.ts` for the spellings; `keyTypeT` in `src/compiler/ts/key-types.ts` and `src/compiler/ts/key-types.test.ts` for the key types, in the compiler and in the language service.

The files that verify it at commit af54ddad5, each at the first line that names the rule:

- [`src/compiler/ts/key-types.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/key-types.test.ts#L1)
- [`src/compiler/ts/key-types.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/key-types.ts#L1)
- [`src/compiler/ts/type-map.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/type-map.ts#L1)
- [`src/core/spec-conformance/surface-names.test.ts:52`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/spec-conformance/surface-names.test.ts#L52)

## Explained in

The sections of the surface document that explain this rule, at commit af54ddad5:

- [§75 String keys and Record](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#75-string-keys-and-record)

## Error codes that enforce it

The diagnostic codes the rule names under Enforced by, or whose registry text names the rule:

**[`TS8002`](https://typeshade.dev/reference/errors/ts8002/) `UNKNOWN_TYPE`**

A type name that nothing declares, in a parameter, a type argument, an alias, an `implements` clause or a base class, or a type that this surface does not take where the mapper reads one ([Rule 2.1](https://typeshade.dev/reference/rules/2-1/)). A base that the file declares and does not collect as a struct is `STRUCT_FIELD`.

## See also

- [Rule 3.9](https://typeshade.dev/reference/rules/3-9/) Test
- [Rule 4.2](https://typeshade.dev/reference/rules/4-2/) Test
- [Rule 1.5](https://typeshade.dev/reference/rules/1-5/)
- [Rule 4.4](https://typeshade.dev/reference/rules/4-4/)
- [Design rules](https://typeshade.dev/reference/rules/)
- [Error codes](https://typeshade.dev/reference/errors/)

## Source

The rule at commit af54ddad5:

- [`docs/language-design.md:331`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/language-design.md#L331) (the design document)
- [`reqs/rules/RULE-0401.md`](https://github.com/typeshade/typeshade/blob/af54ddad5/reqs/rules/RULE-0401.md) (its traceability item)
