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));
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.
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; };
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.
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.
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:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With