Type ‣ Undefined - chung-leong/zigar GitHub Wiki
JavaScript | PHP
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_value } from './undefined-example-1.zig';
console.log(undefined_value);
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());
{ number: 1234, unknown: undefined }
It's different from void, which is a runtime type.