Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在typescript中的forEach下定义类型?_Typescript_Typescript Typings_Typescript2.0_Typescript1.8 - Fatal编程技术网

如何在typescript中的forEach下定义类型?

如何在typescript中的forEach下定义类型?,typescript,typescript-typings,typescript2.0,typescript1.8,Typescript,Typescript Typings,Typescript2.0,Typescript1.8,我有一个PersonTypes对象数组,只想在forEach循环中使用部分键。何况 在typescript中精确、正确地编码以提供类型?我可以做类似的事情 people.forEach((人员:选择 或 people.forEach((person:PersonTypes)=>{ 或 people.forEach((person:any)=>{ 用typescript编写代码的正确方法是什么 export type PersonTypes = { name: string; value:

我有一个
PersonTypes
对象数组,只想在forEach循环中使用部分键。何况 在typescript中精确、正确地编码以提供类型?我可以做类似的事情
people.forEach((人员:选择
people.forEach((person:PersonTypes)=>{
people.forEach((person:any)=>{
用typescript编写代码的正确方法是什么

export type PersonTypes = {
  name: string;
  value: string;
  gender: boolean;
};
const people: PersonTypes[] = [
  {name: 'apl', value: 'apple', gender: true},
  {name: 'gal', value: 'google', gender: false},
]
people.forEach((person: Pick<PersonTypes, 'name' | 'gender'>) =>{
//people.forEach((person: PersonTypes) =>{
//people.forEach((person: any) =>{
  console.log(person.name);
  console.log(person.gender);
} )
导出类型PersonTypes={
名称:字符串;
值:字符串;
性别:布尔型;
};
const people:PersonTypes[]=[
{name:'apl',value:'apple',性别:true},
{name:'gal',value:'google',性别:false},
]
people.forEach((person:Pick)=>{
//people.forEach((person:PersonTypes)=>{
//people.forEach((人员:任意)=>{
console.log(person.name);
console.log(person.gender);
} )

你应该坚持:

people.forEach((person: PersonTypes) =>{


});
这是因为
people
数组中的每个对象都属于
PersonTypes
类型,实际上不需要从该类型中提取属性

事实上,不需要显式地将person键入为
PersonTypes
,因为person属于
PersonTypes[]
。TypeScript将自动推断数组中的每个对象都是
PersonTypes
,因此这就足够了:

people.forEach((person) =>{


});  
或者,您可以选择对参数进行解构,这将使您的函数更加简洁和干净

people.forEach(({ name, gender }) =>{  
  console.log(name);
  console.log(gender);
});

根据您提供的附加代码,
customZip
函数将返回
any
类型,这当然会在以后引起问题,因为数组将具有
any
类型,而不是推断的
PersonType[]

export function customZip(...arrays: Array<any>){
  return arrays
}
……或者

// Your manually inform TypeScript what the type of an array member returned from customZip looks like
const people = [
  ...customZip<PersonTypes[]>([{name: 'apl', value: 'apple', gender: true},
  {name: 'gal', value: 'google', gender: false},])
];
//手动通知TypeScript从customZip返回的数组成员的类型
康斯特人=[
…customZip([{name:'apl',value:'apple',gender:true},
{name:'gal',value:'google',性别:false},])
];

什么是“防止我在公关上被炒鱿鱼”呢?为什么你不能直接使用推断类型,
PersonTypes
,即只写
.forEach(person=>{…});
绑定元素“name”隐式具有“any”类型way@jacobcan118向我们展示如何使用
people.forEach(({name,gender})=>)来分解结构;
对我有效:@Terry我想问题出在我的customZip函数上,我无法真正改变如何解决它?@jacobcan118请更新您的问题:游乐场链接不起作用btw@jacobcan118请更新您的问题:在评论中粘贴链接不会在将来帮助其他人,因为实际的代码与您原来的问题偏离太多(没有提到自定义压缩功能)
// You let TypeScript do the inferring by itself
const people = [
  ...customZip([{name: 'apl', value: 'apple', gender: true},
  {name: 'gal', value: 'google', gender: false},])
];
// Your manually inform TypeScript what the type of an array member returned from customZip looks like
const people = [
  ...customZip<PersonTypes[]>([{name: 'apl', value: 'apple', gender: true},
  {name: 'gal', value: 'google', gender: false},])
];