[英]Type '{ label: string; value: string; }' is missing the following properties from type in Typescript react js
Hi I am getting some datatype error in typescript while getting first record of an array.嗨,我在获取数组的第一条记录时在打字稿中遇到一些数据类型错误。 I am initializing the data like -
我正在初始化数据,如 -
const ABData= testData.B.map(({ id, name }) => ({
label: translateFn!(id),
value : name
}));
const newData: PropsInterface = {
displayData: ABData[0]
};
Below is my data -以下是我的数据——
export const testData: PropsInterface = {
A: [
{ value: "All", key: "All" },
],
B: [
{id: "0", name: ""},
{id: "100000008472", name: "Y6DC"}
],
C: "100000008472",
D: [{id: "0", name: "None"}],
E: [
{value: "", key: "None"},
{value: "A", key: "Test"},
]
}
Here is my interface -这是我的界面 -
export interface PropsInterface {
A: Array<First>;
B: Array<Second>;
C: string;
D: Array<Second>;
E: Array<First>;
}
export interface First{
key: string;
value: string;
}
export interface Second{
id: string;
name: string;
}
I am getting error in displayData if I am trying to get first data using 0th index.如果我尝试使用第 0 个索引获取第一个数据,我会在 displayData 中收到错误。
error is - Type '{ label: string; value: string; }' is missing the following properties from type 'Second[]': length, pop, push, concat, and 26 more.
错误是 - 输入
Type '{ label: string; value: string; }' is missing the following properties from type 'Second[]': length, pop, push, concat, and 26 more.
Type '{ label: string; value: string; }' is missing the following properties from type 'Second[]': length, pop, push, concat, and 26 more.
How can I resolve this error ?我该如何解决这个错误?
First of all lets check your PropsInterface
:首先让我们检查一下你的
PropsInterface
:
// I made A, C, D optionals since the problem here is B as you say first
export interface PropsInterface {
A?: Array<First>;
B: Array<Second>;
C?: string;
D?: Array<Second>;
E?: Array<First>;
}
Then you First
and Second
interfaces:然后你的
First
和Second
接口:
export interface First {
key: string;
value: string;
}
export interface Second {
id: string;
name: string;
}
Now the data you are providing:现在您提供的数据:
export const testData: PropsInterface = {
A: [{value: 'All', key: 'All'}],
B: [
{id: '0', name: ''},
{id: '100000008472', name: 'Y6DC'}
],
C: '100000008472',
D: [{id: '0', name: 'None'}],
E: [
{value: '', key: 'None'},
{value: 'A', key: 'Test'}
]
};
Till here, everything is ok but the problem comes in your .map
function:到这里为止,一切正常,但问题出在您的
.map
函数中:
const ABData = testData.B.map(({id, name}) => ({
label: translateFn!(id),
value: name,
}));
Lets talk in terms of the pure object.让我们谈谈纯对象。 You are assigning a value of type
{label: string, value: string}[]
to the ABData
variable.您正在将
{label: string, value: string}[]
类型的值分配给ABData
变量。 Then you are assigning ABData
to newData
which is of type PropsInterface
.然后,你要分配
ABData
到newData
这类型的PropsInterface
。 You are telling (to TS) that newData
is of type PropsInterface
but this interface doesn't have a displayData
property.你告诉(以TS),其
newData
类型为PropsInterface
但这个接口没有一个displayData
财产。
const newData: PropsInterface = {
displayLabel: ABData[0],
};
So, lets change the name of displayData
prop by B
.因此,让我们通过
B
更改displayData
prop 的名称。
const newData: PropsInterface = {
B: ABData[0]
};
The error is gone but we still have a problem Type '{ label: string; value: string; }' is missing the following properties from type 'Second[]': length, pop, push, concat, and 26 more.ts(2740)
错误消失了,但我们仍然有问题
Type '{ label: string; value: string; }' is missing the following properties from type 'Second[]': length, pop, push, concat, and 26 more.ts(2740)
Type '{ label: string; value: string; }' is missing the following properties from type 'Second[]': length, pop, push, concat, and 26 more.ts(2740)
Type '{ label: string; value: string; }' is missing the following properties from type 'Second[]': length, pop, push, concat, and 26 more.ts(2740)
. Type '{ label: string; value: string; }' is missing the following properties from type 'Second[]': length, pop, push, concat, and 26 more.ts(2740)
。 This is beacause we are assigning an element of an array and not the array itself.这是因为我们分配的是数组的元素而不是数组本身。 So lets change new data with the following:
因此,让我们使用以下内容更改新数据:
const newData: PropsInterface = {
B: ABData
};
This error is gone but we have a new problem Type '{ label: string; value: string; }[]' is not assignable to type 'Second[]'.
这个错误消失了,但我们有一个新问题
Type '{ label: string; value: string; }[]' is not assignable to type 'Second[]'.
Type '{ label: string; value: string; }[]' is not assignable to type 'Second[]'.
, so lets fix it. ,所以让我们修复它。
Second
has id
and name
but we are trying to assign label
and value
. Second
有id
和name
但我们正在尝试分配label
和value
。 For this lets modify the .map
function to this:为此,让我们将
.map
函数修改为:
const ABData = testData.B.map(({id, name}) => ({
id: translateFn!(id),
name: name,
}));
And thats it, we have fix all the errors.就是这样,我们已经修复了所有错误。 The final result is:
最终结果是:
export const testData: PropsInterface = {
A: [{value: 'All', key: 'All'}],
B: [
{id: '0', name: ''},
{id: '100000008472', name: 'Y6DC'}
],
C: '100000008472',
D: [{id: '0', name: 'None'}],
E: [
{value: '', key: 'None'},
{value: 'A', key: 'Test'}
]
};
export interface PropsInterface {
A?: Array<First>;
B: Array<Second>;
C?: string;
D?: Array<Second>;
E?: Array<First>;
}
export interface First {
key: string;
value: string;
}
export interface Second {
id: string;
name: string;
}
const ABData = testData.B.map(({id, name}) => ({
id: translateFn!(id),
name: name
}));
const newData: PropsInterface = {
B: ABData
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.