[英]TypeScript - Partial<T> not working as expected?
以下引發:
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>
}
}
有任何想法嗎?
如果有幫助,請嘗試以下操作:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.