typescript 类型字符串上不存在属性 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41269866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Property id does not exist on type string
提问by Mike3355
I am getting the follow error in my IDE saying Property id does not exist on type string typeScript
for this line of code:
Property id does not exist on type string typeScript
对于这行代码,我在 IDE 中收到以下错误:
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.
这是我的假设,因为我用any
了let customer:any [];
我可以把不同的变量在那里。
----------------- 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));
回答by Piotr Lewandowski
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:
{ name, id, age, city }
ES2015 等效项在哪里:
{
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:
代码价值超过 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);
}