immerjs / use-immer

Use immer to drive state with a React hooks
MIT License
4.04k stars 92 forks source link

v0.5 breaks draft argument typing/type inference with readonly array #79

Closed shimikano closed 3 years ago

shimikano commented 3 years ago

Reproduced here:

https://codesandbox.io/s/use-immer-typing-vp697?file=/src/App.tsx

The example compiles without errors using use-immer-0.4.2.

2021-03-21_08-11-scaled 2021-03-21_08-12-scaled

pip8786 commented 3 years ago

Same with something like this:

export enum ErrorType {
    NoLobby,
    FailCreate,
    BadPassword,
    Rejected
}

export enum SocketState {
    NotConnected,
    Connected
}

type ErrorMap = {
    readonly [key in ErrorType]?: string
};

    const lobbyError = useCallback((message: LobbyErrorMessage) => {
        updateErrorMap(draft => {
            switch (message.subType) {
                case EngageMessageType.CreateLobby:
                    draft[ErrorType.FailCreate] = "Lobby code rejected (duplicate or bad word): " + message.lobbyCode;
                    break;
                case EngageMessageType.LobbyJoined:
                    draft[ErrorType.NoLobby] = "No lobby found with code: " + message.lobbyCode;
                    break;
                case EngageMessageType.GetLobbyInfo:
                    draft[ErrorType.BadPassword] = "The password entered does not match.";
                    break;
            }
        });
    },[updateErrorMap]);

The error is TS2540: Cannot assign to '0' because it is a read-only property. This compiled fine in the last version.

mweststrate commented 3 years ago

Addressed OP in 0.5.2. Thanks for reporting!

@pip8786 didn't check your case. Please check against 0.5.2 and if it doesn't work best open a fresh issue including a CodeSandbox reproduction.