Typescript 部分<;T>;仅适用于内联值
为什么下面的代码的行为与之类似?这是TypeScript编译器中的错误还是缺少功能Typescript 部分<;T>;仅适用于内联值,typescript,partial,keyof,mapped-types,Typescript,Partial,Keyof,Mapped Types,为什么下面的代码的行为与之类似?这是TypeScript编译器中的错误还是缺少功能 class MyType { constructor(public readonly value1: string, public readonly value2: number) { } } function myFunction(props: Partial<MyType>): void { // Do something here } myFunction({ });
class MyType {
constructor(public readonly value1: string, public readonly value2: number) {
}
}
function myFunction(props: Partial<MyType>): void {
// Do something here
}
myFunction({ }); // Compiles
myFunction({ value1: 'string', value2: 42 }); // Compiles
myFunction({ wrongValue: true }); // Compile error!!
const myValue1 = {};
const myValue2 = { value1: 'string', value2: 42 };
const myValue3 = { wrongValue: true };
myFunction(myValue1); // Compiles
myFunction(myValue2); // Compiles
myFunction(myValue3); // Compiles, but why?!? I expected this not to compile!
类MyType{
构造函数(公共只读值1:string,公共只读值2:number){
}
}
函数myFunction(道具:部分):无效{
//在这里做点什么
}
myFunction({});//汇编
myFunction({value1:'string',value2:42});//汇编
myFunction({ErrorValue:true});//编译错误!!
常量myValue1={};
常量myValue2={value1:'string',value2:42};
const myValue3={errowvalue:true};
myFunction(myValue1);//汇编
myFunction(myValue2);//汇编
myFunction(myValue3);//编译,但为什么?!?我以为这不是编译!
我使用了TypeScript 2.1.6版,您要求的是精确的类型,这是跟踪的 目前,TypeScript只会检查过多的对象键来查找对象文字,主要是拼写错误。将对象绑定到变量后,TypeScript不会检查过多的键 规格: Partial有效地将可选标记添加到类的字段中
因此,它不会为
myValue3
报告错误,您要求的是精确的类型,它会被跟踪
目前,TypeScript只会检查过多的对象键来查找对象文字,主要是拼写错误。将对象绑定到变量后,TypeScript不会检查过多的键
规格:
Partial有效地将可选标记添加到类的字段中
因此它不会报告myValue3
的错误