字符串类型上不存在属性id。

21

我在我的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 "";
}

我曾经认为,由于我在 let customer:any []; 中使用了 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));

1
你的假设几乎是正确的。customers 函数正在返回一个字符串数组... - Diogo Sgrillo
2
你可能想要使用 customer.push({name,id,age,city});。你的命名非常混乱。而且你从一个返回类型为 string[] 的函数中返回了类型为 any[] 的 customer。这没有太多意义。 - JB Nizet
1个回答

30

你需要将你的属性放在对象里面:

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[];

除了更好的类型检查,它还能给出更好的语法提示。


编辑:

  • 始终为函数指定返回类型
  • 如果需要,在函数内部尽量不要使用外部变量,而是通过参数传递它们
  • 不要将函数定义与实际代码混合在一起

代码价值千言万语。这里是重构后的版本:

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);
}

3
这有多重帮助。 - Mike3355
2
这是一个非常出色的答案。我发现接口示例特别有帮助。 - rotarydial

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接