Skip to content

Undefined

Chung Leong edited this page Apr 28, 2024 · 7 revisions

The literal undefined in Zig is represented as undefined in JavaScript. You will only encounter it if you explicitly export undefined.

pub const Undefined = undefined;
import { Undefined } from './undefined-example-1.zig';

console.log(Undefined);
undefined

Assigning undefined to a variable would not yield undefined on the JavaScript side.

pub var number: i32 = undefined;
import module from './undefined-example-2.zig';

console.log(module.number);
0

As @TypeOf(undefined) is a comptime type, you can only put it into a struct if you make it a comptime field:

pub const NumberAndUnknown = struct {
    number: i32,
    comptime unknown: @TypeOf(undefined) = undefined,
};
import { NumberAndUnknown } from './undefined-example-3.zig';

const struct = new NumberAndUnknown({ number: 1234 });
console.log(struct.valueOf());

It's different from void, which is a runtime type.

Clone this wiki locally