Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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).
like image 536
rtn Avatar asked Apr 16 '16 13:04

rtn


Video Answer


1 Answers

You should really need to mention which kind of Array you want while defining it, MyClass can be string/number(datatype)

Code

export class TodoService {

  private _todos:Array<MyClass> = [];

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

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

}
like image 90
Pankaj Parkar Avatar answered Sep 28 '22 06:09

Pankaj Parkar