Qui intorno cerchi in cerchio. Molto nuovo a Typescript e causa grossi grattacapi con implementazioni banali.Non so come implementare un enum utilizzando un'interfaccia in una classe angular2
Come definire in AgentStatusService
che dovrebbe disporre di una matrice di 4 opzioni denominate ['offline','available','busy','away']
? AgentStatus è definito (o è?) E lo sto iniettando nello AgentStatusService
.
Microsoft Visual Studio Code è un bar sulla riga 21 in cui il tipo "typeof AgentStatus" non è assegnabile al tipo "AgentStatus" ... perché?
Aggiornato:
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;
// }
}
Confronta con Questo soddisfa implementazione 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;
}
}
Grazie, ho aggiornato la domanda con i tuoi consigli. Ora ottenendo un errore incredibilmente criptico. Tscript sembra felice però. browser_adapter.ts: 78EXCEPTION: Errore: Uncaught (promessa): TypeError: Impossibile leggere la proprietà 'isSkipSelf' di null – Simon