Skip to main content

Function: Optional

Optional(): <T>(target: DecoratorTarget<unknown>, targetKey?: string | symbol, indexOrPropertyDescriptor?: number | TypedPropertyDescriptor<T>) => void

Decorator that marks an optional dependency. Null will be injected if the dependency is not found.

Example

Mark MyService as an optional dependency.

@MnComponent({
scope: {
pageType: "record"
}
})
export class MyComponent {
// Search for a dependency with the identifier "ServiceIdentifier" and inject it into this property.
// If the dependency is not found, null will be injected.
@Optional()
@Inject("ServiceIdentifier")
myService: Service;
}

Returns

fn

▸ <T>(target, targetKey?, indexOrPropertyDescriptor?): void

Type parameters
Name
T
Parameters
NameType
targetDecoratorTarget<unknown>
targetKey?string | symbol
indexOrPropertyDescriptor?number | TypedPropertyDescriptor<T>
Returns

void

Defined in

packages/core/src/core/di/optionnal.ts:25