prime prime - 6 months ago 76
TypeScript Question

Angular2/Typescript - default query string parameters

I have a component that checks for the existence of parameters inside a query string and acts accordingly. However, when I attempt to transpile the code I get errors such as

error TS2339: Property 'oauth_token' does not exist on type '{ [key: string]: any; }'

My component reads as follows

constructor (
private router: Router
) {}

ngOnInit() {

if ( this.router.routerState.snapshot.queryParams.hasOwnProperty('oauth_token') ) {
... do stuff here ...


Can anybody please advise on how to default query string parameters?


Just use the [] operator to access the properties:

const oauthToken = this.router.routerState.snapshot.queryParams['oauth_token'];
if (oauthToken) {
  // ...