簡體   English   中英

打字稿接口擴展

[英]Typescript interface extends

我有一個關於擴展 typescript 接口的問題,類屬性“value”未被識別為數組!

代碼示例:

export class InListFilter<T, D=T[]> extends BaseFilter<T, D> {

    constructor(props: Partial<BaseFilter<T>>) {
        super(props);
    }

    getValue(): string {

        let s: string[] = [];

        if (this.value && this.value.length) {  // <- Property length does not exist on type D
            for (let v of this.value) {
                v = this.getValueParsed(v);
                s.push(this.enquoted ? `'${v}'` : `${v}`);
            }
        }

        return s && s.length ? s.join(',') : '';
    }

    render(): string {

        let s: string = '';

        if (!this.field || !this.hasValue()) {
            return s;
        }

        return `(${this.field} IN (${this.getValue()}))`;
    }
}

非常感謝

來自TypeScript Documentation對泛型約束的描述:

使用 ... extends關鍵字來表示 ... 約束

所以你的例子中的約束是:

export class InListFilter<T, D extends T[]> extends BaseFilter<T, D> {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM