Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

atomWithImmer not resolving the types hint properly #2592

Closed
1 of 2 tasks
khanakia opened this issue Jun 1, 2024 · 0 comments
Closed
1 of 2 tasks

atomWithImmer not resolving the types hint properly #2592

khanakia opened this issue Jun 1, 2024 · 0 comments

Comments

@khanakia
Copy link

khanakia commented Jun 1, 2024

Summary

I have this code and when i mouse hover to immerSetup.screen in vscode it shows any

How do I make it typesafe ?

type Setup = {
  screen: string;
};

const defaultSetup = {
  screen: "step1",
};

// Original atom.
const setupImmerAtom = atomWithImmer<Setup>(defaultSetup);

const setScreen = (screen: Screen) => {
  const [setupAtom, setAtomSetup] = useAtom(setupImmerAtom);
  setAtomSetup((immerSetup) => {
    immerSetup.screen = screen;
    return immerSetup;
  });
};

Screenshot 2024-06-01 at 7 07 33 PM
Screenshot 2024-06-01 at 7 18 47 PM

On debugging further, I found out this

// actual type definition /node_modules/jotai-immer/dist/atomWithImmer.d.ts
export declare function atomWithImmer<Value>(initialValue: Value): WritableAtom<Value, [Value | ((draft: Draft<Value>) => void)], void>;

// resolving as this
atomWithImmer<Setup>(initialValue: Setup): WritableAtom<Setup, [Setup | ((draft: Draft<Value>) => void)], void>

// IT should resolve as this `Draft<Value>` to `Draft<Setup>`
atomWithImmer<Setup>(initialValue: Setup): WritableAtom<Setup, [Setup | ((draft: Draft<Setup>) => void)], void>

By setting the generic type explicitly in atomWithImmer<Setup>, TypeScript should correctly infer Draft<Setup> instead of Draft<Value>.

Link to reproduction

No need just a typescript hint error

Check List

Please do not ask questions in issues.

  • I've already opened a discussion before opening this issue, or already discussed in other media.

Please include a minimal reproduction.

@pmndrs pmndrs locked and limited conversation to collaborators Jun 2, 2024
@dai-shi dai-shi converted this issue into discussion #2593 Jun 2, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant