Unless specified, in this document typeHint
is either null
, undefined
, Number
, String
, Object
, Array
, Symbol
, an instance of Type
, an object literal ({}
) or an array literal ([]
)
(syntax will evolve as the decorator proposal evolve)
class Example {
@typed(Number)
accessor a
@typed(Number, _=> Number)
increment(v) {return v++}
}
the @typed
decorator can take one or multiple typeHint
as parameter
function typed(...typeHint) { /* ... */ }
If some of the typeHints
are not instances of Type
, it will be transformed into one by calling the type
function
If there is no type hint (type()
) will return Type.null
,
if there are multiple type hints, call oneOf
with the same arguments,
if the hint is an object literal ({}
), call recordOf
with it,
if the hint is an array literal ([]
), call tupleOf
with it,
any other hint means only value of the same type will be accepted,
e.g. String only accept strings; Number only accept numbers
// decorator
@typed(Number)
let a
// Typescript
let a: number
Will accept any of the specified typeHints
// decorator
@typed(oneOf(Number, String))
// or
@typed(Number, String)
let a
// Typescript
let a: number | string
When there are multiple typeHints, is equivalent to arrayOf(anyOf(...typeHints))
// decorator
@typed(arrayOf(Number, String))
let a
@typed(arrayOf(Number))
let b
// Typescript
let a: Array<number | string>
let b: Array<number>
Will accept an array with the specified size and type at positions
// decorator
@typed(tupleOf(Number, Number, String))
// or
@typed([Number, Number, String])
let a
// Typescript
let a: [number, number, string]
Will accept an object with the same shape (Unlike other function, this one only accept one argument)
// decorator
@typed(recordOf({a: Number, b: String}))
// or
@typed({a: Number, b: String})
let a
// Typescript
let a: {a: Number, b: String}
Accept null
and undefined
in addition to specified type
When there are multiple typeHints, is equivalent to _null(anyOf(...typeHints))
// decorator
@typed(_null(Number))
let a
// Typescript
let a?: number
Used to type a function :
- the return value is indicated by a lambda returning the wanted
typeHint
- multiple overloads can be set, separated by return values
- if no return value is defined, will default to
Type.null
@typed
will detect when it's decorating a function and don't need the call to func
// decorator
@typed(func(Number, Number, _=> Number))
let a
// or
@typed(Number, Number, _=> Number)
function add(a, b) {return a + b}
// Typescript
let add: (number, number) => number
// or
function add(a: number, b: number): number {}
Will throw when trying to change it's value
Same as never
in typescript
Will accept any value
Same as any
in typescript