---
title: "module(): function in Authoring, TypeShade API reference"
description: "module, a function in Authoring. Assemble a module from its declarations."
url: https://typeshade.dev/api/module/
language: en
---

# `module()`

Function in Authoring

Assemble a module from its declarations.

`import { module } from 'typeshade/core/ir'`

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

## Syntax

```ts
function module(parts: ModuleParts): ModuleDecl
```

## Parameters

**`parts` `ModuleParts`**

the declaration arrays, the optional `funcs` record, and `uses`.

## Return value

`ModuleDecl`

the assembled module.

## Exceptions

**`Error`**

`Error` when a `funcs` record key renames a declaration that was already assembled under a different name, which would corrupt the earlier module’s re-emit.

## Description

`consts`, `structs`, `bindings` and `funcs` are the four arrays a module is made of, and each defaults to empty; `overrides`, `externs`, `enables` and `uses` are the optional rest. The result is a plain [`ModuleDecl`](https://typeshade.dev/api/moduledecl/), the value [`emitModule`](https://typeshade.dev/api/emitmodule/), [`emitGlslModule`](https://typeshade.dev/api/emitglslmodule/), [`reflect`](https://typeshade.dev/api/reflect/) and [`compileModule`](https://typeshade.dev/api/compilemodule/) all take.

The order of `funcs` is the emit order, and two things depend on it. GLSL ES 3.00 requires declare-before-use: the backend topologically sorts its own function section and emits a forward prototype where the call graph forces one, and a callee-first list keeps working without them. A fixed order also keeps the emitted bytes deterministic across rebuilds. A function reached only through a handle call is collected transitively and prepended callee-first, so listing the entry points is usually enough.

`funcs` also accepts a record. Each key renames the handle it holds, an anonymous `fn(params, body)` handle included, and key order is the emit order, since JavaScript preserves string-key insertion order. The record therefore names every function deterministically and no auto-generated `_fn0` name reaches the output, which matters when a snapshot test compares the output or another declaration refers to a function by name. Keep the array form when the list is spread across sources or post-processed as data.

`uses` takes declarator handles, [`uniformStruct`](https://typeshade.dev/api/uniformstruct/), [`storageBuffer`](https://typeshade.dev/api/storagebuffer/), [`resource`](https://typeshade.dev/api/resource/), [`ioStruct`](https://typeshade.dev/api/iostruct/), [`structDecl`](https://typeshade.dev/api/structdecl/), [`constDecl`](https://typeshade.dev/api/constdecl/) and [`fp64Guard`](https://typeshade.dev/api/fp64guard/) among them, and derives the struct, binding and const entries from what each handle already knows. Explicit arrays still work and merge with the derived ones.

## Examples

### Example

```ts
import { module } from 'typeshade/core/ir'

// Array form: the order is the emit order, callees first.
const m = module({ structs: [VsOut.decl], bindings: [U.binding], funcs: [wrap, vs, fs] })

// Record form: each key is the emitted name, and key order is the emit order.
const n = module({ uses: [U, VsOut], funcs: { wrap, vs_main: vs, fs_main: fs } })
```

## In the guide

- [Language guide](https://typeshade.dev/guide/internals/)
- [Emitting and reflection](https://typeshade.dev/guide/internals/emitting-and-reflection/)
- [The CPU oracle](https://typeshade.dev/guide/internals/the-cpu-oracle/)

## See also

- [`ModuleDecl`](https://typeshade.dev/api/moduledecl/)
- [`emitModule`](https://typeshade.dev/api/emitmodule/)
- [`emitGlslModule`](https://typeshade.dev/api/emitglslmodule/)
- [`reflect`](https://typeshade.dev/api/reflect/)
- [`compileModule`](https://typeshade.dev/api/compilemodule/)
- [`uniformStruct`](https://typeshade.dev/api/uniformstruct/)
- [`storageBuffer`](https://typeshade.dev/api/storagebuffer/)
- [`resource`](https://typeshade.dev/api/resource/)
- [`ioStruct`](https://typeshade.dev/api/iostruct/)
- [`structDecl`](https://typeshade.dev/api/structdecl/)
- [`constDecl`](https://typeshade.dev/api/constdecl/)
- [`fp64Guard`](https://typeshade.dev/api/fp64guard/)
- [`fn`](https://typeshade.dev/api/fn/)
- [`ModuleParts`](https://typeshade.dev/api/moduleparts/)
- [`module`](https://typeshade.dev/api/module/)
- [`constExpr`](https://typeshade.dev/api/constexpr/)

## Source

[`src/core/ir/builder.ts`, line 1020](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/ir/builder.ts#L1020), at commit af54ddad5
