TypeScript 错误:类型中缺少属性“0”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44447730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript Error: Property '0' is missing in type
提问by rkt
I have my interface like this
我的界面是这样的
export interface Details {
Name: [{
First: string;
Last: string;
}];
}
I have an observable config variable:
我有一个可观察的配置变量:
Configuration: KnockoutObservable<Details> = ko.observable<Details>();
Configuration: KnockoutObservable<Details> = ko.observable<Details>();
and I would like to assign it a value in the constructor as follows:
我想在构造函数中为它分配一个值,如下所示:
config = {
Name: [{
First: "ABC",
Last: "DEF"
},
{
First: "LMN",
Last: "XYZ"
}]
};
this.Configuration(config);
and I am getting an error:
我收到一个错误:
Types of property 'Name' is incompatible and property '0' is missing in type.
Type '{ First:string; Last:string; }[]' is not assignable to
type '[{ First: string; Last:string; }]'
I don't have control on changing the interface as it is being used elsewhere.
What is the correct way to initialize this config variable ?
我无法控制更改界面,因为它在其他地方使用。
初始化此配置变量的正确方法是什么?
Thanks in advance.
提前致谢。
回答by RyanA91
I came across the same issue and got around it by changing the interface to:
我遇到了同样的问题,并通过将界面更改为:
interface Details {
Name: {
First: string;
Last: string;
}[];
}
I know you may not want the interface changed but hope this helps for anyone that is in this situation.
我知道您可能不希望更改界面,但希望这对处于这种情况的任何人都有帮助。
回答by Greg Gum
This error can come from incorrectly typing an array (like I just did):
这个错误可能来自错误地输入数组(就像我刚刚做的那样):
myArray:[]; //Incorrect, results in error message of `Property '0' is missing in type`
myArray: Array<string>; //Correct
myArray: string[]; //Also correct
The reason is that braces denote a tuple in Typescript, not an array.
原因是大括号表示 Typescript 中的元组,而不是数组。
回答by dbandstra
In this type definition:
在这个类型定义中:
interface Details {
Name: [{
First: string;
Last: string;
}];
}
Name
is not an array at compile-time. It's a tuple with one element. Tuples in Typescript are allowed to have extra elements, but they can't have missing elements. As a 1-tuple, Name
is essentially an array which must have at leastone element.
Name
不是编译时的数组。这是一个只有一个元素的元组。Typescript 中的元组可以有额外的元素,但不能有缺失的元素。作为一个 1 元组,Name
本质上是一个必须至少有一个元素的数组。
However, in this value:
但是,在此值中:
const config = {
Name: [{
First: "ABC",
Last: "DEF"
},
{
First: "LMN",
Last: "XYZ"
}]
};
Since there is no explicit typing, the Name
property here defaults to array type. Arrays can have any number of elements, including zero - which doesn't fit in a 1-tuple. Hence your error.
由于没有显式类型,Name
这里的属性默认为数组类型。数组可以有任意数量的元素,包括零 - 这不适合 1 元组。因此你的错误。
Your error can be fixed by giving the compiler a hint that your literal is actually a tuple:
您可以通过向编译器提示您的文字实际上是一个元组来修复您的错误:
const config: Details = { Name: [{...}, {...}] };
If you do need to be able to take in an array of names, you'll have to do some casting, maybe something like this:
如果您确实需要能够接收一系列名称,则必须进行一些转换,可能是这样的:
if (names.length > 0) {
const config = {
Name: names as Details['Name']
};
Configuration(config);
}
(You could remove the if check if you can determine that the tuple was simply a mistake by whoever wrote the typings.)
(如果您可以确定元组只是编写类型的人的错误,则可以删除 if 检查。)
Tuples reference: https://www.typescriptlang.org/docs/handbook/basic-types.html
元组参考:https: //www.typescriptlang.org/docs/handbook/basic-types.html
回答by Rajaram Nayak
Updating the interface to following should fix the issue :
将界面更新为以下应该可以解决问题:
interface Details {
Name: Array<{
First: string;
Last: string;
}>;
}