awwester
awwester

Reputation: 10182

TypeScript error warning, but it works

I'm using angular2-rc1

import {RouteParams} from '@angular/router-deprecated';

constructor(
    private _routeParams: RouteParams
){}

ngOnInit() {
  var routeParams = this._routeParams.params

  if(routeParams.code){
    console.log('has code param', routeParams.code);
  }
}

In my editor (I'm using atom) it gives me an error for routeParams.code and says Property 'code' does not exist on type {'[key:string]:string; } everything works fine, even the console log logs out the code param. I can't seem to figure out why this is here or how to get rid of it.

Even more annoying is when I start the server (npm start) it gives this error and won't start. I have to comment it out and then the server will start, and then I can uncomment it and it all works fine. Any ideas why this is happening and/or how I can get rid of the typescript error? Let me know any other information to give to help, I'm pretty lost on this.

Upvotes: 0

Views: 103

Answers (1)

eko
eko

Reputation: 40677

It is probably a type error. Since routeParams variable doesn't have a type, typescript is having trouble with its .code property.

You can create an interface for your object: this._routeParams.params.

var routeParams: customObjectDTO = this._routeParams.params;

or you can:

var routeParams: any = this._routeParams.params;

Upvotes: 1

Related Questions