字符串类型中不​​存在属性 id

2024-05-27

我的 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 for 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 });
}

Where { 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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

字符串类型中不​​存在属性 id 的相关文章

随机推荐