Skip to main content
Version: 1.x

@microsoft/fast-foundation > optional

optional variable

A decorator that allows you to optionally inject a dependency depending on whether the [[Key]] is present, for example:

Signature:
optional: (key: any) => any

Example 1

class Foo {
constructor( @inject('mystring') public str: string = 'somestring' )
}
container.get(Foo); // throws

would fail

Example 2

class Foo {
constructor( @optional('mystring') public str: string = 'somestring' )
}
container.get(Foo).str // somestring

if you use it without a default it will inject undefined, so remember to mark your input type as possibly undefined!