---
title: "externVar(): function in Authoring, TypeShade API reference"
description: "externVar, a function in Authoring. Declare a host-provided global: the variable counterpart of externFn."
url: https://typeshade.dev/api/externvar/
language: en
---

# `externVar()`

Function in Authoring

Declare a host-provided global: the variable counterpart of externFn.

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

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

## Syntax

```ts
function externVar<T extends ShaderType>(
  name: string,
  type: T,
  opts?: { spelling?: { wgsl?: string; glsl?: string }; stage?: ExternVarDecl['stage'] },
): ExternVarHandle<KeyOf<T>>
```

## Parameters

**`name` `string`**

the logical symbol name, and the default spelling on both targets.

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

its shader type, for checking every read.

**`opts` optional `{ spelling?: { wgsl?: string; glsl?: string }; stage?: ExternVarDecl['stage'] }`**

per-target `spelling`, and an advisory `stage`.

## Return value

`ExternVarHandle<KeyOf<T>>`

an [`ExternVarHandle`](https://typeshade.dev/api/externvarhandle/): `.node` reads the value, `.decl` goes into `module({ externs })`.

## Description

A host prelude may hand a shader values such as a view matrix or a frame time as loose globals the module does not declare. `externVar` declares such a value abstractly. It emits nothing on either backend; what it provides is everything around the reference: type checking at each use site, a name that survives `mangle`, and an entry in `reflect().requires` (and in a fragment’s `requires`) so a host can check the module’s expectations against what its prelude provides.

`spelling` maps the logical name onto each target. When another host exposes the same value differently, as a WGSL struct member or a bound uniform in place of a GLSL prelude global, the change is confined to the spelling map.

## Examples

### Example

```ts
import { mat4fT } from 'typeshade'
import { externVar } from 'typeshade/core/ir'

const uMatrix = externVar('u_matrix', mat4fT, { stage: 'vertex' })
const clip = uMatrix.node.mul(worldPos) // type-checked; emits `u_matrix * …`
```

## In the guide

- [Migrating a GLSL shader](https://typeshade.dev/guide/internals/migrating-a-glsl-shader/)

## See also

- [`externFn`](https://typeshade.dev/api/externfn/)
- [`ExternVarHandle`](https://typeshade.dev/api/externvarhandle/)
- [`hostUniform`](https://typeshade.dev/api/hostuniform/)

## Source

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