打字稿错误:类型{...}不能分配给'用户[]'

时间:2018-05-04 14:19:00

标签: angular typescript

我开始玩Angular 4和Typescript。我对Typescript完全不熟悉。我看到这个问题被问到here,但我无法提取问题的答案。

import { OnInit } from '@angular/core';

export class UserComponent implements OnInit {
    me: User[];

    ngOnInit() {
       this.me = {
           name: 'Jochen',
           age: 31,
           email: 'mail@web.de',
           address: {
               street: 'Berlinerstraße 24',
               city: 'Berlin',
               state: 'Berlin'
           }
       };
    }
}

interface Address {
    city: string;
    street: string;
    state: string;
}

interface User {
    age: number;
    name: string;
    email: string;
    address: Address;
}

以下是运行ng serve时出现的错误:

ERROR in src/app/user/user.component.ts(11,7): error TS2322: Type '{ name: string; age: number; email: string; address: { street: string; city: string; state: strin...' is not assignable to type 'User[]'.
  Object literal may only specify known properties, and 'name' does not exist in type 'User[]'.

我想知道如何解释第二句话,它说'name' does not exist in type 'User[]'它存在。能帮我理解我的错误吗?

1 个答案:

答案 0 :(得分:4)

for @(%tweets<statuses>) -> $tweet { $tweet<created_at>.say; } 将变量me: User[]声明为me元素数组,我怀疑这不是您想要做的。

正确的语法应该只是User