简体   繁体   English

输入'{标签:字符串; 值:字符串; }' 在 Typescript react js 中缺少 type 中的以下属性

[英]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:然后你的FirstSecond接口:

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 .然后,你要分配ABDatanewData这类型的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 . Secondidname但我们正在尝试分配labelvalue 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.

相关问题 反应选择::输入&#39;{标签:字符串; 值:字符串; }&#39; 缺少类型 &#39;readonly never[]&#39; 中的以下属性 - react-select:: Type '{ label: string; value: string; }' is missing the following properties from type 'readonly never[]' 与 typescript 反应 - Type {} 缺少 type 中的以下属性 - React with typescript - Type {} missing the following properties from type 与 Typescript 反应 - Type { } 缺少 type 中的以下属性 - React with Typescript - Type { } is missing the following properties from type 类型 '(oldPlacePhotos: string[]) =&gt; string[]' 缺少来自类型 'string[]' 的以下属性:pop、push、concat、join 等 28 个 - Type '(oldPlacePhotos: string[]) => string[]' is missing the following properties from type 'string[]': pop, push, concat, join, and 28 more 反应,redux 与 typescript 错误:类型“{}”缺少来自类型“IProps”的以下属性:email,令牌 - react, redux with typescript error: Type '{}' is missing the following properties from type 'IProps': email, token 反应中的 Typescript 错误 - 类型“元素 []”缺少类型“反应元素”中的以下属性<any, any> '</any,> - Typescript error in react - Type 'Element[]' is missing the following properties from type 'ReactElement<any, any>' TypeScript React 功能组件 - 缺少“元素”类型的以下属性:类型、道具、键错误 - TypeScript React Functional Component - is missing the following properties from type 'Element': type, props, key error React-Native 应用程序的 TypeScript 中的类型缺少以下属性 - Type is missing the following properties from type in TypeScript of React-Native app 反应/ TypeScript 类型'ChangeEvent<htmlinputelement | htmltextareaelement> ' 缺少类型 'KeyboardEvent 的以下属性</htmlinputelement> - React/ TypeScript Type 'ChangeEvent<HTMLInputElement | HTMLTextAreaElement>' is missing the following properties from type 'KeyboardEvent TypeScript 错误类型“{}”缺少类型中的以下属性 - TypeScript Error Type '{}' is missing the following properties from type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM