2016-12-21 29 views
2

我得到我的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代替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)); 
+1

你的假設幾乎是正確的。 'customers'函數返回一串字符串... –

+2

您可能需要'customer.push({name,id,age,city});'您的命名非常非常混亂。並且您從返回類型爲string []的函數返回客戶,該客戶的類型爲any []。這沒有什麼意義。 –

回答

6

你有你的包裹凡{ name, id, age, city }是ES2015等價的

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

:內部對象的屬性

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

爲了避免這種錯誤,我傾向於創建interfac Ë是力量結構:

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

這有助於多種方式。 – Drew1208

相關問題