Search code examples
typescriptdecorator

What's this decorator syntax before constructor argument for in typescript?


What's the differences between constructor( @IStorageService storageService: IStorageService) and constructor( storageService: IStorageService) ?

code from vscode first commit on github

import {TPromise} from 'vs/base/common/winjs.base';
import {Remotable, IThreadService} from 'vs/platform/thread/common/thread';
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';

@Remotable.MainContext('MainThreadStorage')
export class MainThreadStorage {

    private _storageService: IStorageService;

    constructor( @IStorageService storageService: IStorageService) {
        this._storageService = storageService;
    }

I have never seen code using decorator syntax in this way ( before constructor's argument ), and I couldn't find typescript doc mentioning this usage.


Solution

  • Parameter decorators are documented at https://www.typescriptlang.org/docs/handbook/decorators.html#parameter-decorators

    A Parameter Decorator is declared just before a parameter declaration. The parameter decorator is applied to the function for a class constructor or method declaration. A parameter decorator cannot be used in a declaration file, an overload, or in any other ambient context (such as in a declare class).

    The expression for the parameter decorator will be called as a function at runtime, with the following three arguments:

    • Either the constructor function of the class for a static member, or the prototype of the class for an instance member.
    • The name of the member.
    • The ordinal index of the parameter in the function’s parameter list.