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

Dre*_*208 7 typescript

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

if(customer.id === id) {//doesn't like customer.id
            return customer;
        }
Run Code Online (Sandbox Code Playgroud)

完整代码:

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 "";
}
Run Code Online (Sandbox Code Playgroud)

这是我的假设,因为我习惯了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));
Run Code Online (Sandbox Code Playgroud)

Pio*_*ski 11

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

function createCustomer(name: string, id: number, age: number, city: string) {
        customer.push({ name, id, age, city });
}
Run Code Online (Sandbox Code Playgroud)

{ name, id, age, city }ES2015 在哪里等效:

{
    id: id,
    name: name,
    age: age,
    city: city
}
Run Code Online (Sandbox Code Playgroud)

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

interface ICustomer {
    id: number;
    name: string;
    age: number;
    city: string;
}
Run Code Online (Sandbox Code Playgroud)

您分配给您的数组:

let customer: ICustomer[];
Run Code Online (Sandbox Code Playgroud)

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


编辑:我已经审核了您的代码并提供了一些有关实践的建议:

  • 始终为函数提供返回类型
  • 尝试不在函数内部处理外部变量,如果需要将它们作为参数传递
  • 不要将函数定义与实际代码混合使用

代码价值超过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);
}
Run Code Online (Sandbox Code Playgroud)

  • 这有助于多种方式. (2认同)
  • 这是一个非常好的答案。我发现界面示例特别有用。 (2认同)