Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Types

SchemaScript has a rich type system with simple types, arrays, nullable types, unions, inline objects, string literals, and generics.

Simple Types

A simple type is an identifier referencing a built-in type, a type alias, or a model name:

id: int
name: string
author: User
status: MessageType

String Literal Types

Quoted strings can be used as types, typically combined with unions to constrain a property to specific values:

type: 'text'|'image'|'video'
status: 'active'|'inactive'|'pending'

Arrays

Append [] to a type to make it an array:

colors: int[]
tags: string[]
messages: Message[]

Nullable Types

Append ? to a type to make the value nullable:

name: string?
avatar_url: string?
items: int[]?

string? means the value can be a string or null. int[]? means the value can be an array of ints or null.

Union Types

Combine types with |:

data: Image|User
result: string|int
type: 'text'|'image'|'video'

Inline Objects

Curly braces define anonymous object types with their own properties:

avatar_image: {
  url: string
  width: int
  height: int
}

last_messages: {
  cached: bool
  data: Message[]
}

Inline objects are expanded inline in generated code – they don’t produce standalone types.

Generic Types

Angle brackets provide type arguments to generic models:

pages: Paginated<User>
config: map<string, string>
nested: map<string, map<string, int>>

See Generics for details on defining and using generic models.

Type Precedence

Type modifiers bind in this order (tightest first):

  1. [] (array)
  2. ? (nullable)
  3. | (union)

This means:

ExpressionMeaning
int[]Array of int
int[]?Nullable array of int
string|intString or int
string|int?String, or nullable int

Parentheses

Parentheses override the default precedence:

// Array of nullable int
items: (int?)[]

// Nullable union
value: (string|int)?

// Array of arrays of nullable int
matrix: ((int?)[])[]

Without parentheses, int?[] would mean “nullable array of int”. With parentheses, (int?)[] means “array of nullable int”.