Decorators
TypeSpec decorators are implemented as JavaScript functions. The process of creating a decorator can be divided into two parts:
- Declare the decorator signature in TypeSpec (optional but recommended)
- Implement the decorator in JavaScript
Declare the decorator signature
While this step is optional, it offers significant benefits:
- It enables type checking for the parameters
- It provides IDE IntelliSense
You can declare a decorator signature using the dec
keyword. Since weβre implementing the decorator in JavaScript (the only option currently), we need to use the extern
modifier as well.
Specifying the decorator target
The first parameter of the decorator represents the TypeSpec type(s) that the decorator can be applied to.
You can specify multiple potential target types using a union expression
.
Optional parameters
You can mark a decorator parameter as optional using ?
.
Rest parameters
You can prefix the last parameter of a decorator with ...
to collect all the remaining arguments. The type of this parameter must be an array expression
.
Value parameters
A decorator parameter can receive values by using the valueof
operator. For example the parameter valueof string
expects a string value. Values are provided to the decorator implementation according the decorator parameter marshalling rules.
JavaScript decorator implementation
Decorators can be implemented in JavaScript in 2 ways:
- Prefixing the function name with
$
. e.gexport function $doc(target, name) {...}
Great to get started/play with decorators - Exporting all decorators for your library using
$decorators
variable. Recommended
A decorator implementation takes the following parameters:
1
:context
of typeDecoratorContext
2
:target
The TypeSpec type target. (Namespace
,Interface
, etc.)3+
: Any arguments of the decorators.
Or in JavaScript:
The decorator can then be used like this:
Decorator parameter marshalling
When decorators are passed types, the type is passed as-is. When a decorator is passed a TypeSpec value, the decorator receives a JavaScript value with a type that is appropriate for representing that value.
TypeSpec value type | Marshalled type in JS |
---|---|
string | string |
boolean | boolean |
numeric | Numeric or number (see below) |
null | null |
enum member | EnumMemberValue |
When marshalling numeric values, either the Numeric
wrapper type is used, or a number
is passed directly, depending on whether the value can be represented as a JavaScript number without precision loss. In particular, the types numeric
, integer
, decimal
, float
, int64
, uint64
, and decimal128
are marshalled as a Numeric
type. All other numeric types are marshalled as number
.
When marshalling custom scalar subtypes, the marshalling behavior of the known supertype is used. For example, a scalar customScalar extends numeric
will marshal as a Numeric
, regardless of any value constraints that might be present.
Legacy value marshalling
With legacy value marshalling, TypeSpec strings, numbers, and booleans values are always marshalled as JS values. All other values are marshalled as their corresponding type. For example, null
is marshalled as NullType
.
TypeSpec Value Type | Marshalled value in JS |
---|---|
string | string |
numeric | number |
boolean | boolean |
Note that with legacy marshalling, because JavaScript numbers have limited range and precision, it is possible to define values in TypeSpec that cannot be accurately represented in JavaScript.
String templates and marshalling
If a decorator parameter type is valueof string
, a string template passed to it will also be marshalled as a string.
The TypeSpec type system will already validate the string template can be serialized as a string.
Typescript type Reference
TypeSpec Parameter Type | TypeScript types |
---|---|
valueof string | string |
valueof numeric | number |
valueof boolean | boolean |
string | StringLiteral | TemplateLiteral | Scalar |
Reflection.StringLiteral | StringLiteral |
Reflection.TemplateLiteral | TemplateLiteral |
Adding metadata with decorators
Decorators can be used to register some metadata. For this, you can use the context.program.stateMap
or context.program.stateSet
to insert data that will be tied to the current execution.
β Do not save the data in a global variable.
Reporting diagnostic on decorator or arguments
The decorator context provides the decoratorTarget
and getArgumentTarget
helpers.
Linking declaration and implementation
Decorator signatures are linked to the implementation of the same name in the same namespace.
This is linked to the following in lib.js
:
Troubleshooting
Extern declaration must have an implementation in JS file
Potential issues:
- The JS function is not prefixed with
$
. For a decorator called@decorate
, the JS function must be called$decorate
. - The JS function is not in the same namespace as the
extern dec
. - Is the error only showing in the IDE? Try restarting the TypeSpec server or the IDE.
You can use --trace bind.js.decorator
to log debug information about decorator loading in the JS file, which should help identify the issue.