如何在使用TypeScript将元素推入数组时提供类型定义?

huangapple go评论72阅读模式
英文:

How to provide type definition while pushing the element into array using TypeScript?

问题

我需要避免在将元素推入数组时使用'any'作为类型定义。我尝试提供类型但出现了错误。以下是示例代码:

interface answerProps {
    state: string;
    answer: string;
}

const results: Array<string> = [];
!isEmpty(answers) && answers.map((item: any) => results.push(`${item.state} : ${item.answer}`));

根据上述代码,我希望在映射数组元素时避免使用'any'
英文:

I need to avoid the using of 'any' as type definition while pushing the element into array. I tried to provide the type but I am getting an error. Here is the sample code

interface answerProps {
    state: string;
    answer: string;
  }

  const results: Array&lt;string&gt; = [];
  !isEmpty(answers) &amp;&amp;
    answers.map((item: any) =&gt; results.push(`${item.state} : ${item.answer}`));

As per the above code , I wanted to avoid of using any while mapping the array element.

答案1

得分: 1

只需在映射数组时使用answerProps接口。

interface answerProps {
  state: string;
  answer: string;
}

const results: Array<string> = [];
!isEmpty(answers) && answers.map((item: answerProps) => results.push(`${item.state} : ${item.answer}`));
英文:

Just use the answerProps interface while mapping the array.

interface answerProps {
  state: string;
  answer: string;
}

const results: Array&lt;string&gt; = [];
!isEmpty(answers) &amp;&amp;
  answers.map((item: answerProps) =&gt; results.push(`${item.state} : ${item.answer}`));

答案2

得分: 0

interface answerProps {
  state: string;
  answer: string;
}

const answers: answerProps[] = [];
const results = answers.map(item => `${item.state} : ${item.answer}`);

note 1
有两种方式可以进行类型数组的类型转换。对于我的示例,我主要使用了第二种类型转换方法

  • const arr1: Array<string>
  • const arr2: string[]

note 2
在你提供的示例中,array.map 不是正确的方法,尝试使用 array.forEach 代替

note 3
当正确使用array.map时,TypeScript可以自动识别结果类型,并且非常适用于避免手动将类型转换为变量

note 4
(这仅仅是个人偏好)与其防范空数组,尝试重构你的代码,使其接受空数组作为有效输入。在我上面提供的示例中,一个空的 answers 将映射为一个空的 results


<details>
<summary>英文:</summary>

```typescript
interface answerProps {
  state: string;
  answer: string;
}

const answers: answerProps[]  = []
const results = answers.map(item =&gt; `${item.state} : ${item.answer}`);

note 1
there are 2 ways of casting a typed array. For my example, I mainly used the 2nd casting method

  • const arr1: Array&lt;string&gt;
  • const arr2: string[]

note 2
in your given sample, array.map is not the correct method to use, try using array.forEach instead

note 3
array.map when used correctly, allows typescript to automatically identify the resulted type, and is great to avoid manually casting type to variables

note 4
(this is purely preferences) rather then guarding against empty array, try to refactor your code so that it will accepts empty array as valid. In the example I have given above, an empty answers will gets mapped to an empty results

答案3

得分: 0

只返回翻译好的部分:

const results: Array<string> = [];
!isEmpty(answers) && answers.map((item: answerProps ) => results.push(`${item.state} : ${item.answer}`));

如果仍然显示错误请尝试为数组提供数据类型如定义数组 `let answers:answerProps[]`  `answers as answerProps[]`但请确保具有相同类型的数据

顺便说一下在定义类或接口时请使用大写字母开头

例如`AnswerProps`
英文:
const results: Array&lt;string&gt; = [];
  !isEmpty(answers) &amp;&amp;
    answers.map((item: answerProps ) =&gt; results.push(`${item.state} : ${item.answer}`));

Change any to answerProps if it is the same type.

If it still shows an error, then try to provide a data type for the array such as define array let answers:answerProps[] or answers as answerProps[]. But please make sure to have data with the same type.

By the way, use uppercase for the first letter when you define a class or an interface

ex: AnswerProps

huangapple
  • 本文由 发表于 2023年7月18日 14:27:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定