Building Types with @Type
Construct new types programmatically.
Building Types with @Type is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Reverse of @typeInfo
If @typeInfo turns a type into data, @Type turns data back into a real type. Together they let you reshape types at compile time. 🛠️
Feed It a Type.Info Value
@Type takes a std.builtin.Type value and produces the concrete type it describes. You hand it a description, it gives you a type.
const T = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = 8 } });Build an Integer Type
The example above constructs u8 from scratch. Change bits to 16 and you get u16, all decided while the program compiles.
const U16 = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = 16 } });Round-Trip a Type
Passing a type through @typeInfo then @Type returns the very same type. This proves the two builtins are exact inverses.
const Same = @Type(@typeInfo(u32));Describe a Struct as Data
To build a struct you fill in a StructField array. Each field needs a name, a type, alignment, and a default pointer.
const f = std.builtin.Type.StructField;Assemble the Struct Payload
Wrap your fields in a .Struct payload with a layout and an is_tuple flag, then hand the whole thing to @Type.
const info = .{ .Struct = .{
.layout = .auto,
.fields = my_fields,
.decls = &.{},
.is_tuple = false,
} };Generate a Fresh Struct Type
Call @Type on that payload and you get a brand-new struct type, built by your own logic rather than written by hand.
const Generated = @Type(info);Defaults Need a Comptime Pointer
A field's default_value is an optional const pointer to the value, or null when there is no default. The pointer must be comptime-known.
const dv: ?*const anyopaque = &@as(u8, 0);Build Enums and More
The same trick works beyond structs: the .Enum payload lets @Type synthesize enum types from a computed list of members.
Why Construct Types at All
Generated types power serializers and ORMs: derive a packed config or row type automatically instead of editing it by hand. ✨
Pair It with Reflection
The strongest pattern reads a type with @typeInfo, transforms the data, then rebuilds a new type with @Type, like adding a field.
Quick Check
You have a std.builtin.Type value describing a struct. What turns it into a usable type?
Recap
@Type builds real types from Type.Info data, the inverse of @typeInfo. Pair them to read, transform, and regenerate types. 🎯
Frequently asked questions
Is the “Building Types with @Type” lesson free?
Yes — the full text of “Building Types with @Type” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building Types with @Type”?
Construct new types programmatically. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building Types with @Type” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Reflecting Fields with @typeInfo
- Building Types with @Type
- Comptime Validation and @compileError
- Generating Code with comptime