TypeScript - 部分<t>无法按预期工作?

时间:2018-01-16 11:27:39

标签: javascript typescript

以下投掷:

Type '{ wheels: 4; }' is not assignable to type 'Partial<T>'.

以下是代码:

interface Vehicle{
    wheels: number;
}

class Driver<T extends Vehicle>{
    car: Partial<T>;
    constructor(){
        this.car = {wheels: 4} 
        // Type '{ wheels: 4; }' is not assignable to type 'Partial<T>'.
        // (property) Driver<T extends Vehicle>.car: Partial<T>
    }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果有帮助的话,试试这个:

interface Vehicle {
    wheels: number
}
type DumpDriver = Partial<Vehicle>;
class Driver<T extends Vehicle> {
    car: DumpDriver;
    constructor(){
        this.car = { wheels: 4 };
    }
}
var doubleCross = new Driver();
console.log(doubleCross.car);

class WhichDriver<T extends Vehicle> {
    wheels: number;
    constructor(){
        this.wheels = 4;
    }
}
var whereIsTheBaggage = new WhichDriver();
console.log(whereIsTheBaggage.wheels);