返回承诺的 TypeScript 属性 - Get/Set 访问器必须具有相同的类型

TypeScript properties returning a promise - Get/Set accessors must have the same type

为什么 TypeScript 强制 Get/Set 访问器具有相同的类型? 假设我想要一个 属性 其中 returns 一个承诺。

module App {
    export interface MyInterface {
        foo: ng.IPromise<IStuff>;
    }

    export interface IStuff {
        bar: string;
        baz: number;
    }

    class MyClass implements MyInterface {
        private _fooDeferred: ng.IDeferred<IStuff>;

        constructor(private $q: ng.IQService) {
            this._fooDeferred = this.$q.defer();
        }

        get foo(): ng.IPromise<IStuff> {
            return this._fooDeferred.promise;
        }

        set foo(value: IStuff) {
            this._fooDeferred.resolve(value);
        }
    }
}

'Get' 和 'Set' 访问器必须具有相同的类型 将是来自 TypeScript 的错误消息。

修复方法是将访问器类型化为 any,但这样我们就失去了静态类型化的优势,还不如只写 JS。

        get foo(): any {
            return this._fooDeferred.promise;
        }

        set foo(value: any) {
            this._fooDeferred.resolve(value);
        }

这听起来像是使用联合类型(TypeScript 1.4 或更高版本)的绝好机会 - 来自 this blog post 的示例:

type StringPromise = string | ng.IPromise<string>;

module App {
    export interface MyInterface {
        foo: ng.IPromise<string>;
    }

    class MyClass implements MyInterface {
        private _fooDeferred: ng.IDeferred<string>;

        constructor(private $q: ng.IQService) {
            this._fooDeferred = this.$q.defer();
        }

        get foo(): StringPromise {
            return this._fooDeferred.promise;
        }

        set foo(value: StringPromise) {
            this._fooDeferred.resolve(value);
        }
    }
}

备注:

  • 当您想使用联合类型中的特定类型时,您将需要使用类型保护
  • 在某些情况下您可能需要使用类型断言

类型保护

这是一个类型保护的例子

if (typeof value === 'string') {
    // the type of value inside this if statement is
    // string, rather than StringPromise
} else {
    // the type of value inside this else statement is
    // ng.IPromise<string>, rather than StringPromise
}

类型断言

如果需要,您可以这样断言类型:

var prom = <string> value;