在打字稿中定义对象数组

我想在打字稿中定义一个对象数组:


const a = [{

  name: 1,

  age: 2,

  car1: 8,

  car2: 8,

  car3: 8,

  name4: 1,

  age4: 2,

  car41: 8,

  car42: 8,

  car34: 8,

},

{

  name: 1,

  age: 2,

  car1: 8,

  car2: 8,

  car3: 8,

  name4: 1,

  age4: 2,

  car41: 8,

  car42: 8,

  car34: 8,

}

]


const fun = (obj: Array < object > ) => {

  console.log(obj)

}


fun(a)

就我而言,使用这种结构是正确的:obj: Array <object>或者我应该定义对象的每个键?



慕森王
浏览 90回答 3
3回答

繁星淼淼

问题的解决方案取决于您想要编程的场景!。以下是您的代码可能出现的一些情况。定义一个对象并从中推断其键。const persons = [&nbsp; { name: "John", age: 12 },&nbsp; { name: "Ben", age: 20 }];const fun = (info: typeof persons) => {&nbsp; //You will get intellisense here&nbsp; console.log(info[0].name);};您想要具有固定键的对象,在这种情况下可以使用类型和接口。interface IPerson {&nbsp; id?: string; // ID is optional (use of ? operator)&nbsp; name: string; // Name is Required&nbsp; age: number;}const persons: Array<IPerson> = [&nbsp; { name: "John", age: 12 },&nbsp; { name: "Ben", age: 20 }];// Both are same: Array<IPerson> === IPerson[]const fun = (info: Array<IPerson>) => {&nbsp; //You will get intellisense here&nbsp; console.log(info[0].name);};您想要拥有具有固定键的对象,并且想要提供部分信息。interface IPerson {&nbsp; id?: string; // ID is optional (use of ? operator)&nbsp; name: string; // Name is Required&nbsp; age: number;}const persons: Array<Partial<IPerson>> = [&nbsp; { name: "John" }, // You can do it.&nbsp; { name: "Ben", age: 20 }];// Both are same: Array<IPerson> === IPerson[]const fun = (info: Partial<IPerson>[]) => {&nbsp; //You will get intellisense here&nbsp; console.log(info[0].name);};附加信息,Typescript 不支持运行时类型检查,仅支持编译时类型检查。对于运行时验证,您可以按如下方式实现该函数:const is_valid_person = (person: any): Boolean => {&nbsp; return (&nbsp; &nbsp; typeof person === "object" &&&nbsp; &nbsp; typeof person.name === "string" &&&nbsp; &nbsp; typeof person.age === "number" &&&nbsp; &nbsp; person.name.length >= 5 &&&nbsp; &nbsp; person.age >= 1&nbsp; );};console.log("Is person valid: ", is_valid_person({}));console.log("Is person valid: ", is_valid_person("Invalid Person"));我希望上述方法之一能够解决您的问题。就我而言,使用以下结构是正确的: obj: Array 或者我应该定义对象的每个键?上述问题的答案是:您可以使用上面显示的任何一种方法,因为 typescript 可以帮助您编写更好的代码并在编译时减少错误。一旦你的程序被编译,执行的代码就是纯 JavaScript。并且 JavaScript 不会验证您的响应。上述所有模式都会生成相同的JavaScript代码,因此不存在性能问题。

神不在的星期二

您可以创建自己的自定义对象类型:type CustomObject = {&nbsp; name: number&nbsp; age: number&nbsp; car1: number&nbsp; car2: number&nbsp; car3: number&nbsp; name4: number&nbsp; age4: number&nbsp; car41: number&nbsp; car42: number&nbsp; car34: number}const arrayOfCustomObjects: CustomObject[] = [{&nbsp; name: 1,&nbsp; age: 2,&nbsp; car1: 8,&nbsp; car2: 8,&nbsp; car3: 8,&nbsp; name4: 1,&nbsp; age4: 2,&nbsp; car41: 8,&nbsp; car42: 8,&nbsp; car34: 8,},{&nbsp; name: 1,&nbsp; age: 2,&nbsp; car1: 8,&nbsp; car2: 8,&nbsp; car3: 8,&nbsp; name4: 1,&nbsp; age4: 2,&nbsp; car41: 8,&nbsp; car42: 8,&nbsp; car34: 8,}]const fun = (objs: CustomObject[]) => {&nbsp; &nbsp; objs.forEach((obj) => {&nbsp; &nbsp; &nbsp; &nbsp; console.log(obj)&nbsp; &nbsp; })}fun(arrayOfCustomObjects)

狐的传说

正如您在文档中看到的,使用object可能有点懒。假设数组中的所有对象都具有相同的属性,您可以像这样解决这个问题:interface UserWithCars {  name: number;  age: number;  // All other properties}const fun = (objectArray: Array<UserWithCars>): void => {  console.log(objectArray);}如果所有对象都是同一类型,您甚至可以创建一个类(这应该是实际的方法):class UserWithCars {  name: number;  age: number;  // All other properties  constructor(/* etc */) {    // constructor operations  }}const fun = (objectArray: Array<UserWithCars>): void => {  console.log(objectArray);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript