# Foreign Types

A foreign type is one you don't own and can't annotate: `DateTime` from `effect`, `Decimal` from a
math library, and so on. Instead of adding `encodeWith` to every field that uses it, configure it
once in `macroforge.config.ts` and every derive picks it up.

```typescript
// macroforge.config.ts
import { DateTime } from 'effect';

export default {
    foreignTypes: {
        'DateTime.Utc': {
            from: ['effect'],
            encode: (v) => DateTime.formatIso(v),
            decode: (raw) => DateTime.unsafeFromDate(new Date(raw)),
            default: () => DateTime.unsafeNow(),
            hasShape: (v) => typeof v === 'string'
        }
    }
};
```

Now any field typed `DateTime.Utc` encodes through your handlers.

## Options

| Key       | Purpose                                                |
| --------- | ------------------------------------------------------ |
| `from`    | Module paths this type may be imported from            |
| `aliases` | Additional `{ name, from }` pairs naming the same type |

The rest are handlers, one for each function a built-in derive generates for a type of your own and
calls on a field of it:

| Handler          | Derive       | Signature                                                         |
| ---------------- | ------------ | ----------------------------------------------------------------- |
| `encode`         | `Encode`     | `(value: T) => unknown`, the JSON-safe form                       |
| `decode`         | `Decode`     | `(raw: unknown) => T`, throwing on what it cannot read            |
| `default`        | `Default`    | `() => T`                                                         |
| `hasShape`       | `Decode`     | `(raw: unknown) => boolean`: whether raw input could be this type |
| `is`             | `Encode`     | `(value: unknown) => value is T`: whether a value is this type    |
| `clone`          | `Clone`      | `(value: T) => T`                                                 |
| `toString`       | `Debug`      | `(value: T) => string`                                            |
| `equals`         | `PartialEq`  | `(a: T, b: T) => boolean`                                         |
| `hashCode`       | `Hash`       | `(value: T) => number`                                            |
| `partialCompare` | `PartialOrd` | `(a: T, b: T) => number \| null`                                  |
| `compare`        | `Ord`        | `(a: T, b: T) => number`                                          |

A derive that needs a handler the type does not configure fails to expand, naming the handler. A
field is never copied, compared, hashed or printed by its structure in place of the handler.

## In a union with your own types

A field can hold a foreign type or one of your own, as `RecordId | Author` does, often through a
generic alias such as `type Ref<T> = /** @default */ RecordId | T` declared in another module.
Decoding asks `hasShape` of the raw input, which may be the wire form; encoding asks `is` of the
live value:

```typescript
hasShape: (v): v is RecordId | string => v instanceof RecordId || typeof v === "string",
is: (v): v is RecordId => v instanceof RecordId
```

What they accept goes through the foreign handlers; everything else goes through your type's own
encoder and decoder.

## Import-source validation

`from` exists to disambiguate types that share a name across packages. A `Decimal` from `decimal.js`
and one from your own module are different types that happen to collide.

When a type's name matches a foreign-type entry but its **import source doesn't**, the match falls
through to generic handling **silently**: the type may be a different one, and TypeScript reports
any mismatch. If a foreign type appears not to be applying, check that the module path in `from`
matches how the file actually imports it.

When the name and source match but the qualification doesn't, such as `DateTime` for
`"DateTime.DateTime"`, expansion warns and names the alias that would match it.

## Namespaced names

A dotted key like `"DateTime.Utc"` is interpreted as namespace `DateTime`, type `Utc`. The namespace
is derived from the key: there's no separate `namespace` option to set. To match the type under
another name or path, list it in `aliases`.

## Setup

Generated code imports your handlers from `#macroforge/config`, which your manifest has to declare
once:

```bash
macroforge init
```

Expansion fails with an error naming the missing entry until it is declared.
[Expanded Config](/docs/endec/foreign-types/expanded-config) shows what the handlers become and why,
with generated code before and after.
