我想在Angular 5应用程序中使用异步管道 . 我'm defining an Observable with users in the controller. When I'm使用异步管道在模板中展开它,Angular语言服务无法识别类型 UserModel 并且似乎正在使用 any .

我的问题:如何让异步管道与类型一起使用?

根据this post它应该工作 .

Angular: v5.5.1
Angular Language Service: v5.5.1
WebStorm: 2017.3.2

What doesn't work:

@Controller({})
export class MyController {
  users: Observable<UserModel[]>
  constructor() { this.users = userService.get() }
}

模板:

<div *ngFor="let user in users | async">{{ user.firstName + user.missing }}</div>

My current alternative

@Controller({})
export class MyController extends OnDestroy {
  private alive = true
  users: UserModel[]
  constructor() { 
    userService
      .get()
      .takeWhile(() => this.alive())
      .subscribe(users => this.users = users) 
  }
  ngOnDestroy() { this.alive = false; }
}

模板:

<div *ngFor="let user in users">{{ user.firstName + user.missing }}</div>

EDIT: 执行 userService.get()

get() { return this.http.get<UserModel[]>('api/users') )}

EDIT2: 添加了一个plunkr . 要清楚 - 我想在 user 上为属性获取构建错误和自动完成功能 . 所以: user.name 应该可以正常工作,但 user.missing 应该抛出构建错误 . https://plnkr.co/edit/i9ggczzIFPEpP30KrcmE?p=preview