I’m trying to load a JSON file into a Array of class objects. But I receive the following error:
Argument of type ‘(room: IRoomManager) => RoomManager’ is not
assignable to parameter of type ‘(value: { id: number; name: string;
description: string; participants: never[]; capacity: number; status:
boolean; }, index: number, array: { id: number; name: string;
description: string; participants: never[]; capacity: number; status:
boolean; }[]) => RoomManager’. Types of parameters ‘room’ and
‘value’ are incompatible.
Type ‘{ id: number; name: string; description: string; participants: never[]; capacity: number; status: boolean; }’ is
missing the following properties from type ‘IRoomManager’: getId,
getName, getDescription, getParticipants, and 7 more.
Is this caused because I added the functions from the RoomManager class to the interface or what is causing this issue?
initializeRooms = () => {
this.rooms = RoomConfig.map(
(room: IRoomManager) =>
new RoomManager(
room.id,
room.name,
room.description,
room.participants,
room.capacity,
room.status,
),
);
logger.gameDebugMessage(`${this.rooms.length} rooms initialized`);
};
which is supposed to load a imported JSON file:
const rooms = [
{
id: 1,
name: 'Room 1',
description: 'Example',
participants: [],
capacity: 20,
status: true,
},
{
id: 2,
name: 'Room 2',
description: 'Example',
participants: [],
capacity: 20,
status: true,
},
];
export default rooms;
and write all rooms to a array: rooms: RoomManager[];
which is of the type of the RoomManager class:
import IRoomManager from '../interfaces/IRoomManager';
import IClientRoomData from '../interfaces/IClientRoomData';
import PlayerManager from './player.manager';
class RoomManager implements IRoomManager {
id: number;
name: string;
description: string;
participants: PlayerManager[];
capacity: number;
status: boolean;
constructor(
id: number,
name: string,
description: string,
participants: PlayerManager[],
capacity: number,
status: boolean,
) {
this.id = id;
this.name = name;
this.description = description;
this.participants = participants;
this.capacity = capacity;
this.status = status;
}
getId = (): number => this.id;
getName = (): string => this.name;
getDescription = (): string => this.description;
getParticipants = (): PlayerManager[] => this.participants;
getCapacity = (): number => this.capacity;
getStatus = (): boolean => this.status;
setDescription = (description: string) => {
this.description = description;
};
setParticipants = (participants: PlayerManager[]) => {
this.participants = participants;
};
setCapacity = (capacity: number) => {
this.capacity = capacity;
};
setStatus = (status: boolean) => {
this.status = status;
};
generateRoomList = (): IClientRoomData => {
return {
id: this.id,
name: this.name,
description: this.description,
participant_count: this.participants.length,
capacity: this.capacity,
status: this.status,
};
};
}
export default RoomManager;
The implemented interface looks like the following:
import PlayerManager from './IPlayerManager';
import IClientRoomData from './IClientRoomData';
interface IRoomManager {
id: number;
name: string;
description: string;
participants: PlayerManager[];
capacity: number;
status: boolean;
getId: () => number;
getName: () => string;
getDescription: () => string;
getParticipants: () => PlayerManager[];
getCapacity: () => number;
getStatus: () => boolean;
setDescription: (description: string) => void;
setParticipants: (participants: PlayerManager[]) => void;
setCapacity: (capacity: number) => void;
setStatus: (status: boolean) => void;
generateRoomList: () => IClientRoomData;
}
export default IRoomManager;
1