Going around in circles here. Very new to Typescript and it's causing major headaches with trivial implementations.
How do a define in AgentStatusService
that it should have an array of 4 options called ['offline','available','busy','away']
? AgentStatus is defined ( or is it? ) and I am injecting it into the AgentStatusService
.
Microsoft Visual Studio Code is barfing on line 21 where the type 'typeof AgentStatus' is not assignable to type 'AgentStatus'... why?
Updated:
import { EventEmitter, Injectable } from '@angular/core';
export enum AgentStatus {
available =1 ,
busy = 2,
away = 3,
offline = 0
}
export interface IAgentStatusService {
state: number
states: AgentStatus
}
@Injectable()
export class AgentStatusService implements IAgentStatusService {
state:number; // this really should be string, but line 22 returns a number
states:AgentStatus;
constructor(states:typeof AgentStatus = AgentStatus){
// unreacheable code browser_adapter.ts:78EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot read property 'isSkipSelf' of null
// absolutely impossible to debug...
this.state = AgentStatus.offline // this returns a number
}
// set state(state:string){
// try{
// this._state = this.states[state];
// // string
// } catch(e){
// console.log('tried setting bad enum value on AgentStatus', e.stack);
// }
// }
// get state():string{
// return this._state;
// }
// get model():any {
// return this.states;
// }
}
Compare with This implementation satisfies angular2:
@Injectable()
export class AgentStatusService {
public states = ['offline','available','busy','away'];
private _state;
constructor(){
this._state = this.states[0];
}
set state(state:string){
try{
this._state = this.states[state];
} catch(e){
console.log('tried setting bad enum value on AgentStatus', e.stack);
}
}
get state():string{
return this._state;
}
get model():any {
return this.states;
}
}
You have
states:AgentStatus = AgentStatus
. Herestates:AgentStatus
is actually a value of type AgentStatus where as= AngentStatus
is the whole enum.Fix
you probably want:
Or
More
This is similar to you try to assign
foo:SomeClass = SomeClass
instead offoo:SomeClass = new SomeClass()
.It's not obvious what you want here, but let me explain a few things...
I have created a blog post talking about this:
How to use TypeScript Enums, especially with Angular 2+
But I'll include all the information inline here:
A enum is just an object. Your enum is written something like this in JavaScript:
The benefit from typing is very limited in enums.
This line is valid:
But it's not useful, because
So, you should always store your values as numbers, which you can obtain as follows:
How to convert string to enum
This makes the previous comparison work:
Now, a couple questions remain:
How do you get the string equivalent?
How do you get all possible enum values?
Gotcha
If you write this in your template:
It will fail, because it doesn't have access to imported types (it gets executed later by Angular).
To make it work, your component will need to have a reference to the enum type / object, something like:
Runnable Demo
Here is an example that explains everything I pointed in here:
http://plnkr.co/edit/vOeeDrCI6CmsXdWaMtwG?p=preview
Look in the file:
app/app.component.ts
.