---
title: "constDecl(): function in Layout, TypeShade API reference"
description: "constDecl, a function in Layout. Declare a module-level scalar constant together with its typed reference in one call."
url: https://typeshade.dev/api/constdecl/
language: en
---

# `constDecl()`

Function in Layout

Declare a module-level scalar constant together with its typed reference in one call.

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

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

## Syntax

```ts
function constDecl<T extends ShaderType>(
  name: string,
  type: T,
  values: { readonly wgsl: number; readonly cpu: number },
): ConstHandle<T>
```

## Parameters

**`name` `string`**

the emitted constant name.

**`type` `T extends ShaderType`**

the constant’s scalar type.

**`values` `{ readonly wgsl: number; readonly cpu: number }`**

`wgsl`, the literal WGSL emits, and `cpu`, the value the CPU oracle uses.

## Return value

`ConstHandle<T>`

the [`ConstHandle`](https://typeshade.dev/api/consthandle/), `.decl` and `.node`.

## Description

WGSL emits `values.wgsl` as the literal, in the same spelling a hand-written `ConstDecl` uses (for example `PI = 3.14159265`). The CPU oracle, the same module run in double precision on the CPU, evaluates `values.cpu` at full JS double precision, so a WGSL literal you deliberately shorten does not lower the precision of the parity check.

Returns a [`ConstHandle`](https://typeshade.dev/api/consthandle/). Its `.decl` goes into `module({ consts })` or, more usually, into `module({ uses })`, which collects the `.decl` of any handle passed to it. Its `.node` is the typed reference to use at call sites, so a renamed or misspelled constant is a `tsc` error everywhere it is used. For a constant of vector, matrix or array type (a `vec4<f32>` colour, an `array<vec4<f32>, N>` palette) use [`constExpr`](https://typeshade.dev/api/constexpr/), which takes one constant-foldable literal node in place of the WGSL/CPU value pair.

## Examples

### Example

```ts
const TAU = constDecl('TAU', f32T, { wgsl: 6.2831853, cpu: Math.PI * 2 })
const g = fn('g', { t: f32T }, ({ t }) => t.mul(TAU.node))
const m = module({ consts: [TAU.decl], funcs: [g] })
```

## See also

- [`ConstHandle`](https://typeshade.dev/api/consthandle/)
- [`constExpr`](https://typeshade.dev/api/constexpr/)
- [`array`](https://typeshade.dev/api/array/)

## Source

[`src/core/sot.ts`, line 76](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/sot.ts#L76), at commit af54ddad5
