## Type-Specific Encoding

A field is encoded by the whole structure of its type, to any depth:

| Type                                                                                | Encoding Strategy                                                                                                                                                                                                                                      |
| ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Primitives, literals and their unions                                               | The value as it is                                                                                                                                                                                                                                     |
| `Date`                                                                              | `toISOString()`                                                                                                                                                                                                                                        |
| Arrays, readonly or not                                                             | An array of its elements, each encoded by the element type; left as it is when the elements are                                                                                                                                                        |
| `Set<T>`, `ReadonlySet<T>`                                                          | An array of its elements, each encoded by `T`                                                                                                                                                                                                          |
| `Map<K,V>`, `ReadonlyMap<K,V>`                                                      | An object of its entries, each value encoded by `V`. A key is its text: a string, number, bigint, boolean, an enum or newtype of one, or a `Date`, written as its ISO string. Any other key type is an expansion error                                 |
| `Record<K,V>`                                                                       | An object of its members, each value encoded by `V`                                                                                                                                                                                                    |
| Tuples                                                                              | An array of its elements, each encoded by its own type, optional and rest elements included                                                                                                                                                            |
| An object type written in place, `{ at: Date }`                                     | The object, each member encoded by its own type                                                                                                                                                                                                        |
| `T \| null`, `T \| undefined`                                                       | `null` and `undefined` as they are, anything else as `T`                                                                                                                                                                                               |
| `Required<T>`, `Readonly<T>`, `NonNullable<T>`                                      | As the inner type `T`                                                                                                                                                                                                                                  |
| `Partial<T>`, `Pick<T,K>`, `Omit<T,K>`                                              | `nameEncodePartialWithContext`, which encodes the fields the value has and, for a `Pick` or `Omit`, only those the type keeps. Such a value is written out wherever it is held and never becomes a `__ref`; one that holds itself throws a `TypeError` |
| A type deriving `Encode`                                                            | Its `nameEncodeWithContext` function                                                                                                                                                                                                                   |
| A configured foreign type                                                           | Its `encode` handler                                                                                                                                                                                                                                   |
| A type parameter                                                                    | The encoder handed in for it, or the value as it is without one                                                                                                                                                                                        |
| A type named through a namespace import, `models.User`                              | An expansion error: import the type by name                                                                                                                                                                                                            |
| `string \| User`, a primitive beside one type deriving `Encode`                     | The primitive as it is, anything else through the type                                                                                                                                                                                                 |
| Any other union with a member that is encoded, `Circle \| Square`, `Date \| number` | An expansion error: nothing in a field says which member a value is. Name the union with an alias and derive on that                                                                                                                                   |
| `keyof T`, `T['field']` over a type the project declares                            | As what the operator yields                                                                                                                                                                                                                            |
| A function, a symbol, `void`, `never`, a mapped, conditional or `typeof` type       | An expansion error: code the field with `encodeWith`, or leave it out with `skip`                                                                                                                                                                      |
| `unknown`, `any`, a type the project does not declare                               | The value as it is                                                                                                                                                                                                                                     |

A number has to be finite: `encode` throws a `TypeError` for `NaN` and the infinities, which JSON
would write as `null`. A class field whose declaration leaves its type unsaid (`items = []`) is an
expansion error: annotate it, or code it with `encodeWith`.

JSON has no `undefined`. A field that holds one is left out. In an array, a tuple or a `Set`, and as
the value of a `Record` or `Map` entry, one is written as `null`, which `decode` reads back as
`undefined` where the type there takes `undefined` and not `null`.

Note: which strategy applies is resolved **statically** from the field's declared TypeScript type at
expansion time: there is no runtime feature detection.
