gpt4 book ai didi

typescript - 类型字符串上不存在属性 ID

转载 作者:搜寻专家 更新时间:2023-10-30 20:39:03 25 4
gpt4 key购买 nike

我在我的 IDE 中收到以下错误,提示 Property id does not exist on type string typeScript 这行代码:

if(customer.id === id) {//doesn't like customer.id
return customer;
}

完整代码:

let customer:any [];

function Customers(): string[] {

let id = 0;

createCustomer("Drew",id++,22,"Glassboro");
createCustomer("Mike",id++,40,"Rineyville");
createCustomer("Justin",id++,19,"Jonesboro");
createCustomer("Alex",id++,15,"Paulsboro");
createCustomer("Phil",id++,32,"Glassboro");

return customer;
}

function createCustomer(name:string,id:number,age:number,city:string){
customer.push(name,id,age,city);
}

const allCustomers = Customers();

function getCustomerInformation(id:number): string {

for (let customer of allCustomers) {

if(customer.id === id){
return customer;
}

}

return "";
}

这是我的假设,因为我将 any 用于 let customer:any []; 我可以在其中放置不同的变量。

---------------- 感谢您的帮助,这是我的新解决方案--------

interface ICustomer{
id: number;
name: string;
age: number
city: string
}

let customers: Array<ICustomer>;

function generateCustomers(): void {

let id: number = 0;

createCustomer("Drew", id++, 22, "Glassboro");
createCustomer("Mike", id++, 40, "Rineyville");
createCustomer("Justin", id++, 19, "Jonesboro");
createCustomer("Alex", id++, 15, "Paulsboro");
createCustomer("Phil", id++, 32, "Glassboro");

}

function getAllCustomers(): ICustomer[]{

generateCustomers();

return customers;
}

function createCustomer(name:string,id:number,age:number,city:string): void {

let newCustomer:ICustomer = {id:id,name:name,age:age,city:city};

customers.push(newCustomer);
}

const allCustomers = getAllCustomers;

function getCustomerInformation(id:number): ICustomer {

for (let customer of allCustomers()) {

if(customer.id === id){
return customer;
}
}

return null;
}


console.log(getCustomerInformation(1));

最佳答案

您必须将您的属性包装在对象中:

function createCustomer(name: string, id: number, age: number, city: string) {
customer.push({ name, id, age, city });
}

其中 { name, id, age, city} 是 ES2015 等价的:

{
id: id,
name: name,
age: age,
city: city
}

为了避免这种错误,我倾向于创建强制结构的界面:

interface ICustomer {
id: number;
name: string;
age: number;
city: string;
}

你分配给你的阵列:

let customer: ICustomer[];

除了更好的类型检查,它还为您提供了更好的语法提示。


编辑:我已经审查了您的代码并提出了一些关于实践的建议:

  • 总是给函数返回类型
  • 尽量不要在函数内部使用外部变量,如果需要将它们作为参数传递
  • 不要将函数定义与实际代码混合

代码超过 1000 字。这是重构后的版本:

const allCustomers: ICustomer[] = customers();

interface ICustomer {
id: number;
name: string;
age: number;
city: string;
}

function customers(): ICustomer[] {
let id: number = 0;

return [
createCustomer(id++, "Drew", 22, "Glassboro"),
createCustomer(id++, "Mike", 40, "Rineyville"),
createCustomer(id++, "Justin", 19, "Jonesboro"),
createCustomer(id++, "Alex", 15, "Paulsboro"),
createCustomer(id++, "Phil", 32, "Glassboro")
];
}

function createCustomer(id: number, name: string, age: number, city: string): ICustomer {
return { id, name, age, city };
}

function getCustomerInformation(customers: ICustomer[], id: number): ICustomer {
// Note undefined is returned if object not found
return customers.find(customer => customer.id === id);
}

关于typescript - 类型字符串上不存在属性 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41269866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com