3 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
問題的解決方案取決于您想要編程的場景!。以下是您的代碼可能出現(xiàn)的一些情況。
定義一個(gè)對(duì)象并從中推斷其鍵。
const persons = [
{ name: "John", age: 12 },
{ name: "Ben", age: 20 }
];
const fun = (info: typeof persons) => {
//You will get intellisense here
console.log(info[0].name);
};
您想要具有固定鍵的對(duì)象,在這種情況下可以使用類型和接口。
interface IPerson {
id?: string; // ID is optional (use of ? operator)
name: string; // Name is Required
age: number;
}
const persons: Array<IPerson> = [
{ name: "John", age: 12 },
{ name: "Ben", age: 20 }
];
// Both are same: Array<IPerson> === IPerson[]
const fun = (info: Array<IPerson>) => {
//You will get intellisense here
console.log(info[0].name);
};
您想要擁有具有固定鍵的對(duì)象,并且想要提供部分信息。
interface IPerson {
id?: string; // ID is optional (use of ? operator)
name: string; // Name is Required
age: number;
}
const persons: Array<Partial<IPerson>> = [
{ name: "John" }, // You can do it.
{ name: "Ben", age: 20 }
];
// Both are same: Array<IPerson> === IPerson[]
const fun = (info: Partial<IPerson>[]) => {
//You will get intellisense here
console.log(info[0].name);
};
附加信息,Typescript 不支持運(yùn)行時(shí)類型檢查,僅支持編譯時(shí)類型檢查。
對(duì)于運(yùn)行時(shí)驗(yàn)證,您可以按如下方式實(shí)現(xiàn)該函數(shù):
const is_valid_person = (person: any): Boolean => {
return (
typeof person === "object" &&
typeof person.name === "string" &&
typeof person.age === "number" &&
person.name.length >= 5 &&
person.age >= 1
);
};
console.log("Is person valid: ", is_valid_person({}));
console.log("Is person valid: ", is_valid_person("Invalid Person"));
我希望上述方法之一能夠解決您的問題。
就我而言,使用以下結(jié)構(gòu)是正確的: obj: Array 或者我應(yīng)該定義對(duì)象的每個(gè)鍵?
上述問題的答案是:
您可以使用上面顯示的任何一種方法,因?yàn)?typescript 可以幫助您編寫更好的代碼并在編譯時(shí)減少錯(cuò)誤。一旦你的程序被編譯,執(zhí)行的代碼就是純 JavaScript。并且 JavaScript 不會(huì)驗(yàn)證您的響應(yīng)。
上述所有模式都會(huì)生成相同的
JavaScript
代碼,因此不存在性能問題。

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以創(chuàng)建自己的自定義對(duì)象類型:
type CustomObject = {
name: number
age: number
car1: number
car2: number
car3: number
name4: number
age4: number
car41: number
car42: number
car34: number
}
const arrayOfCustomObjects: CustomObject[] = [{
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 = (objs: CustomObject[]) => {
objs.forEach((obj) => {
console.log(obj)
})
}
fun(arrayOfCustomObjects)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊
正如您在文檔中看到的,使用object
可能有點(diǎn)懶。
假設(shè)數(shù)組中的所有對(duì)象都具有相同的屬性,您可以像這樣解決這個(gè)問題:
interface UserWithCars {
? name: number;
? age: number;
? // All other properties
}
const fun = (objectArray: Array<UserWithCars>): void => {
? console.log(objectArray);
}
如果所有對(duì)象都是同一類型,您甚至可以創(chuàng)建一個(gè)類(這應(yīng)該是實(shí)際的方法):
class UserWithCars {
? name: number;
? age: number;
? // All other properties
? constructor(/* etc */) {
? ? // constructor operations
? }
}
const fun = (objectArray: Array<UserWithCars>): void => {
? console.log(objectArray);
}
添加回答
舉報(bào)