Looking for assistance with building an Angular2 application that can receive messages via WebSocket. These messages vary in content but always include a message identifier. To handle this, I am aiming to implement a child-parent architecture. However, I encounter an error when trying to execute the following code snippet. Any help would be appreciated.
Typescript code
export interface Message {
id: String
}
export interface PlayerData extends Message {
age: String
}
export interface ScoreInfo extends Message {
tBest: number,
tPlayer: number
}
@Injectable()
export class GameCommunicationService {
public messages: Subject<Message>;
constructor(wsService: WebsocketService) {
this.messages = <Subject<Message>>wsService
.connect(CHAT_URL)
.map((response: MessageEvent): Message => {
let data = JSON.parse(response.data);
if (data.id == 12) {
return {
id: data.id,
age: data.age
}
} else if (data.id == 13) {
return {
id: data.id,
tBest: data.tBest,
tPlayer: data.tPlayer
}
} else {
return {
id: data.id
}
}
});
}
}
Error:
ERROR in D:/angular/src/app/game-communication.service.ts (57,7): Type '{ code: any; age: any; }' is not assignable to type 'Message'.
Object literal may only specify known properties, and 'age' does not exist in type 'Message'.
ERROR in D:/angular/src/app/game-communication.service.ts (57,15): Cannot find name 'age'.
ERROR in D:/angular/src/app/game-communication.service.ts (57,7): Type '{ code: any; age: any; }' is not assignable to type 'Message'.
Object literal may only specify known properties, and 'age' does not exist in type 'Message'.
ERROR in D:/angular/src/app/game-communication.service.ts (57,15): Cannot find name 'age'.
....