Values
TypeSpec can define values in addition to types. Values are useful in an API description to define default values for types or provide example values. They are also useful when passing data to decorators, and for template parameters that are ultimately passed to decorators or used as default values.
Values cannot be used as types, and types cannot be used as values, they are completely separate. However, string, number, boolean, and null literals can be either a type or a value depending on context (see also scalar literals). Additionally, union and enum member references may produce a type or a value depending on context (see also enum member & union variant references).
Value kinds
There are four kinds of values: objects, arrays, scalars. and null. These values can be created with object values, array values, scalar values and initializers, and the null literal respectively. Additionally, values can result from referencing enum members and union variants.
Object values
Object values use the syntax #{}
and can define any number of properties. For example:
The object value’s properties must refer to other values. It is an error to reference a type.
Array values
Array values use the syntax #[]
and can define any number of items. For example:
As with object values, array values cannot contain types.
If an array type defines a minimum and maximum size using the @minValue
and @maxValue
decorators, the compiler will validate that the array value has the appropriate number of items. For example:
Scalar values
There are two ways to create scalar values: with a literal syntax like "string value"
, and with a scalar initializer like utcDateTime.fromISO("2020-12-01T12:00:00Z")
.
Scalar literals
The literal syntax for strings, numerics, booleans and null can evaluate to either a type or a value depending on the surrounding context of the literal. When the literal is in type context (a model property type, operation return type, alias definition, etc.) the literal becomes a literal type. When the literal is in value context (a default value, property of an object value, const definition, etc.), the literal becomes a value. When the literal is in an ambiguous context (e.g. an argument to a template or decorator that can accept either a type or a value) the literal becomes a value. The typeof
operator can be used to convert the literal to a type in such ambiguous contexts.
Scalar initializers
Scalar initializers create scalar values by calling an initializer with one or more values. Scalar initializers for types extended from numeric
, string
, and boolean
are called by adding parenthesis after the scalar reference:
Any scalar can additionally be declared with named initializers that take one or more value parameters. For example, utcDateTime
provides a fromISO
initializer that takes an ISO string. Named scalars can be declared like so:
Null values
Null values are created with the null
literal.
The null
value, like the null
type, doesn’t have any special behavior in the TypeSpec language. It is just the value null
like that in JSON.
Const declarations
Const declarations allow storing values in a variable for later reference. Const declarations have an optional type annotation. When the type annotation is absent, the type is inferred from the value by constructing an exact type from the initializer.
The typeof
operator
The typeof
operator returns the declared or inferred type of a value reference. Note that the actual value being stored by the referenced variable may be more specific than the declared type of the value. For example, if a const is declared with a union type, the value will only ever store one specific variant at a time, but typeof will give you the declared union type.
Validation
TypeSpec will validate values against built-in validation decorators like @minLength
and @maxValue
.
Enum member & union variant references
References to enum members and union variants can be either types or values and follow the same rules as scalar literals. When an enum member reference is in type context, the reference becomes an enum member type. When in value context or ambiguous context the reference becomes the enum member’s value.
When a union variant reference is in type context, the reference becomes the type of the union variant. When in value context or ambiguous context the reference becomes the value of the union variant. It is an error to refer to a union variant whose type is not a literal type.