Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property id does not exist on type string

Tags:

typescript

I am getting the follow error in my IDE saying Property id does not exist on type string typeScript for this line of code:

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

full code:

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

It was my assumption since I used any for let customer:any []; I could put different variables in there.

----------------- Thanks for some help this is my new solution--------

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));
like image 469
Mike3355 Avatar asked Dec 21 '16 18:12

Mike3355


People also ask

How do you fix property does not exist on type?

The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names.

Does not exist on type string TS 2339?

To fix the error "TS2339: Property 'x' does not exist on type 'Y'" with TypeScript, we should make sure the properties are listed in the interface that's set as the type of the object. interface Images { main: string; [key: string]: string; } const getMainImageUrl = (images: Images): string => { return images. main; };

Does not exist in type string []?

To fix the "Property 'includes' does not exist on type 'string[]'" error with TypeScript, we can add the 'es2017' option to the compilerOptions. lib option in tsconfig. json . to add "es2017" into the lib array so that the TypeScript compiler knows that ES2017 features are available for use in our TypeScript project.

Does not exist on type void?

The "Property does not exist on type void" error occurs when we try to access a property on the return value of a function that doesn't return anything. To solve the error, make sure to return the correct value from all of the function's code paths.


1 Answers

You have to wrap your properties inside of object:

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

Where { name, id, age, city } is ES2015 equivalence of:

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

To avoid this kind of mistakes, I tend to create interface that forces structure:

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

which you assign to your array:

let customer: ICustomer[];

Except better type checking, it gives you better syntax hints.


Edit: I've reviewed your code and made few suggestions about practices:

  • Always give return type to functions
  • Try to not work on external variables inside functions, if needed pass them as parameters
  • Don't mix function definitions with actual code

Code worth more than 1000 words. Here is refactored version:

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);
}
like image 108
Piotr Lewandowski Avatar answered Oct 25 '22 13:10

Piotr Lewandowski