# Literal Type
Literal Type is an enumerated type. You can use multiple strings, numbers, Boolean values, or nil literals as each item of the enumerated type. When assigning a value to an enumerated type, you must use a compatible enumerated type or enumeration. The literal value in the type, otherwise an error is reported during compilation.
The type of the enumeration type at runtime is consistent with the type of the specific value at runtime, that is, the enumeration type is a compile-time type, and there is no enumeration type at runtime
The enumeration type has no constructor, which is different from the record type
grammar
type EnumTypeName = LiteralValue1 | LiteralValue2 ...
LiteralValue1 can use LiteralString | LiteralNumber | LiteralInteger | LiteralBool | nil any of these literals
- E.g:
type Gender = "male" | "female"
var gender: Gender
gender = "male" -- Correct
gender = "Chinese" -- Error: Compile Error
function a1(p: true)
end
function a2(p: 'Chinese')
end
let a3: string = gender -- Variables of enumerated type can be explicitly downgraded to the type of enumerated value
-- Literal Type can also be stitched
type Cat = "whiteCat" | "blackCat"
type Dog = "husky" | "corgi"
type Pets = Cat | Dog
-- Equivalent to type Pets = "whiteCat" | "blackCat" | "husky" | "corgi"
type Falsy = "" | 0 | false | nil