# truefit/emit

> Every export of truefit/emit, with its signature and doc comment: emit, emitWith, typeOf, plus 4 types.

Source: https://truefit.interlace.tools/docs/api/emit

<!-- Generated by scripts/api-reference.ts from the built dist/*.d.ts. Do not edit; run `npx tsx scripts/api-reference.ts`. -->

```ts
import { emit, emitWith, typeOf } from 'truefit/emit';
```

## Constants

### emit

JavaScript for a function `(value) => boolean` that answers what `check(schema, value)` answers.
It is one expression, so `export const isOrder = ${emit(Order)};` is a module. With
`{ typescript: true }` it is TypeScript for the guard `(value: unknown) => value is T` instead, so
a `.ts` module that holds it type-checks under `strict`, and narrows where it is called.

```ts
const emit: <T>(schema: Schema<T, unknown>, options?: EmitOptions) => Source<Guard<T>>;
```

### emitWith

The same, for any schema: source for a function that takes the schema's own functions, in
`hooks`, and returns the guard. `./jit` is what calls it.

```ts
const emitWith: <T>(schema: Schema<T, unknown>, options?: EmitOptions) => {
    source: Source<(hooks: readonly unknown[]) => Guard<T>>;
    hooks: readonly unknown[];
};
```

### typeOf

The TypeScript type of what the schema accepts, and the aliases it needs: a schema that names
itself stands by the alias `<prefix>_<n>`, so declare each beside the type, as
`type <name> = <text>;`. `prefix` is `T` unless given; give the name the type is declared under,
so that two types in one module have aliases of their own.

```ts
const typeOf: (schema: Node, prefix?: string) => TypeText;
```

## Interfaces

### EmitOptions

```ts
interface EmitOptions {
    /**
     * TypeScript in place of JavaScript: every parameter typed, and the function a guard,
     * `(v: any): v is T`, with `T` the type the schema validates written out as `typeOf` writes it.
     * The checks are the same text, so what it answers and how fast are the same as the JavaScript's.
     */
    readonly typescript?: boolean;
    /**
     * With `typescript`: the name `T` is declared under beside the text (`typeOf(schema, name)`
     * writes it), which the guard names in place of the type written out inside the text.
     */
    readonly type?: string;
}
```

### TypeText

The type a schema validates, as TypeScript text (gap A-20261118-validator-emit-untyped): what
`emit` writes after `v is` in a typed guard, and what `apicontract gen validator` writes beside
each validator as `export type`.

```ts
interface TypeText {
    /** The type, with each alias by its name. */
    readonly text: string;
    /** The alias of each schema that names itself, by name, in the order they are finished. */
    readonly aliases: ReadonlyMap<string, string>;
}
```

## Types

### Guard

A function that answers whether a value is a `T`, and says so to the type system.

```ts
type Guard<T> = (value: unknown) => value is T;
```

### Source

Source text, and, in the type system only, what it evaluates to.

```ts
type Source<F> = string & {
    readonly '~evaluates'?: F;
};
```
