打字稿接口扩展

时间:2021-07-13 13:14:33

标签: typescript

我有一个关于扩展 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()}))`;
    }
}

非常感谢

1 个答案:

答案 0 :(得分:0)

来自 TypeScript Documentation 对泛型约束的描述:

<块引用>

使用 ... extends 关键字表示 ... 约束

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

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