简体   繁体   English

打字稿getter和setter错误

[英]Typescript getter and setter error

Ok it's my first day doing some Angular 2 using typescript and I am try to make a simple getter and setter service. 好吧,这是我第一天使用打字稿做一些Angular 2,我试着做一个简单的getter和setter服务。

import {Injectable} from "angular2/core";

@Injectable()
export class TodoService {

  private _todos:Array = [];

  get todos():Array {
    return this._todos;
  }

  set todos(value:Array) {
    this._todos = value;
  }

}

Can anyone explain why the Typescript compiler is throwing the following error as I think it should be ok. 任何人都可以解释为什么Typescript编译器抛出以下错误,因为我认为它应该没问题。

ERROR in [default] /Users/testguy/WebstormProjects/angular2-seed/src/app/services/todo-service.ts:6:17 
Generic type 'Array<T>' requires 1 type argument(s).

ERROR in [default] /Users/testguy/WebstormProjects/angular2-seed/src/app/services/todo-service.ts:8:14 
Generic type 'Array<T>' requires 1 type argument(s).

ERROR in [default] /Users/testguy/WebstormProjects/angular2-seed/src/app/services/todo-service.ts:12:18 
Generic type 'Array<T>' requires 1 type argument(s).

You should really need to mention which kind of Array you want while defining it, MyClass can be string / number (datatype) 在定义它时你真的需要提到你想要的那种ArrayMyClass可以是string / number (数据类型)

Code

export class TodoService {

  private _todos:Array<MyClass> = [];

  get todos():Array<MyClass> {
    return this._todos;
  }

  set todos(value:Array<MyClass>) {
    this._todos = value;
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM