lloyAlloy on GitHub
Interfaces and types
Docs

Interfaces and types

Interfaces, mapped types, and type functions

Updated

interface is sugar over type with extends. Partial, Readonly, and Sink are language-level types. Type functions that the real solver runs build them, and a file may replace any of them with its own.

Alloyinterfaces.aly
1import { Partial } from "@alloy/std/types"
2
3interface Entity extends Named, Positioned
4    read id: number
5    write sink: (string) -> ()
6end
7local function patch(e: Entity, changes: Partial<Entity>): Entity
8    return { ...e, ...changes }
9end
Emitted Luausame line count
1local __alloy = require("@alloy")
2
3type Entity = Named & Positioned & { read id: number, write sink: (string) -> () }
4
5
6
7local function patch(e: Entity, changes: __alloy.Partial<Entity>): Entity
8    return __alloy.spread( e, changes )
9end return nil
  • T[], read T[], write T[] name Array<T>, ReadArray<T>, and WriteArray<T>.
  • { [K in keyof T]: T[K]? } and its read and write forms compile to generated type functions.
  • type function Keys(t) works as in Luau, and hovers as type function Keys.
  • Hover keeps your annotation, changes: Partial<Entity>, instead of the analyzer's expansion.