Home > Software design >  Typescript filter no signatures
Typescript filter no signatures

Time:12-06

I have a typescript error that say .filter no signatures. I am not sure how to fix this

interface IDevice {
    deviceId: string;
    deviceName?: string;
}

const joinRoom = ({ userId, deviceId, deviceName }: IRoomParams) => {
  rooms[userId] = rooms[userId]?.filter((id) => id !== deviceId);
})

Update: below I added all of my interface and the full function for joining a room. I am not sure how to structure my types so that I can use .filter to remote device from the list when device disconnects

const rooms: Record<string, Record<string, IDevice>> = {};
interface IDevice {
    deviceId: string;
    deviceName?: string;
}
interface IRoomParams extends IDevice {
    userId: string;
}

interface ISendRequestParams {
    userId: string;
    options: any;
    requestId: string;
}

interface IReturnRequestParams {
    userId: string;
    data: any;
    requestId: string;
    error: any;
}

const joinRoom = ({ userId, deviceId, deviceName }: IRoomParams) => {
    if (!rooms[userId]) rooms[userId] = {};
    // console.log('device joined the room', userId, deviceId, deviceName);
    rooms[userId][deviceId] = { deviceId, deviceName };
    socket.join(userId);
    
    io.sockets.to(userId).emit('get-devices', {
        userId,
        participants: rooms[userId]
    });

    socket.on('disconnect', () => {
        console.log(`user left the room: roomId[${userId}], device[${deviceId}], deviceName[${deviceName}]`);
        rooms[userId] = rooms[userId]?.filter((id) => id !== deviceId);
        socket.to(userId).emit('device-disconnected', deviceName);
    });
};

error: This expression is not callable. Type 'IDevice' has no call signatures.ts(2349)

CodePudding user response:

If you want to remove the deviceId from your Record (dictionary) of devices associated to a userId, you can just use the delete operator:

The delete operator removes a property from an object.

delete rooms[userId][deviceId];
  • Related