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 從公開儲存庫中收錄這些內容。

檢舉或申請下架