Roblox Luau Types

TabooHarmony/roblox-brain/skills/core/roblox-luau-types

作者 TabooHarmony38826be57ee37bcf023e9c2b85681bea3909281c无许可证收录于 2026年10月9日更新于 2026年10月9日

Use for Luau annotations, generics, unions, narrowing, strictness, sealed tables, module type exports, typed metatables, or new-solver features.

AI 生成的概览

关于 Roblox Luau 类型标注、泛型、联合类型、类型收窄、严格模式与新求解器特性的参考指南。

功能
该技能提供 Roblox 开发中 Luau 类型系统的精简参考。内容涵盖严格模式指令、新类型求解器开关、推断原则、密封与非密封表、联合类型与标签联合类型、类型收窄、泛型、模块类型导出、带类型的元表、类型转换、信任边界以及常见错误。它指向一份更完整的参考文档以获取示例。
适用场景
适用于编写或审查涉及类型标注、泛型、联合类型、类型收窄、严格模式设置、密封表、模块类型导出、带类型元表或新求解器特性(如 keyof 与类型函数)的 Luau 代码时。它不面向通用 Luau 语法或面向对象与异步模式,那些由其他技能处理。
运行要求
无需脚本或运行时依赖,属于纯指令型技能。它引用随附的参考文档(references/full.md)并列出外部文档来源,因此阅读完整参考需要访问该文件。

Luau Type System

When to Load

Load for Luau types: annotations, generics, unions, narrowing, sealed/unsealed tables, strictness (--!strict vs --!nonstrict), module type exports, metatable typing, and new-solver features (keyof, setmetatable<T, M>, type functions, read). For syntax, roblox-luau-core; for OOP/async, roblox-luau-patterns.

Quick Reference

Strictness: --!strict for maintained code, --!nonstrict while transitioning, --!nocheck only for legacy or generated code. Directives and project settings select the mode; never assume one global default.

New solver gate: keyof, rawkeyof, setmetatable<T, M>, type functions, and read members need the new type solver. On by default for nocheck/nonstrict projects; strict projects need Workspace.UseNewLuauTypeSolver = Enabled (Scripting). Errors here usually mean wrong solver, not wrong syntax.

Inference philosophy: Infer first; annotate boundaries (params, returns, exports). Don't annotate every local.

Sealed vs unsealed tables: An unannotated local table stays open to new fields until annotated, returned from a function, or its scope exits; annotation seals it. Build tables fully before annotating; passing a table to a typed function does not seal it.

Unions: local id: string | number is a union; prefer tagged unions (type State<T> = {kind:"loading"} | {kind:"ready", value:T}) and discriminate on kind to narrow.

Narrowing: typeof(v) == "string" narrows primitives, instance:IsA("BasePart") narrows Instances, assert(v, "msg") removes nil. Discriminants narrow tagged unions; full reference has worked examples.

Generics: Use when input→output type matters: function first<T>(list: {T}): T?, or type Result<T> = {success: boolean, value: T?}. Never replace with any.

Type exports: export type Foo = {...} at module boundaries; consumers use Types.Foo.

Object typing: typeof(setmetatable({} :: CounterData, Counter)) types precise self; the new solver adds setmetatable<T, M> directly.

Casts (::): For narrowing overly generic inference, never for hiding errors.

Trust boundaries: Annotations contract with the compiler, not runtime data. Remotes, DataStores, HttpService, and attributes still need checks; inside a trusted boundary, let types carry the load.

Key mistakes: any propagation in nonstrict, sealing too early, unions without discriminants, annotating every local, deleting new-solver syntax without checking the solver setting.

Full reference: see references/full.md

来源与署名

来源:TabooHarmony/roblox-brain位于skills/core/roblox-luau-types提交38826be

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架