pmndrs / jotai

👻 Primitive and flexible state management for React
https://jotai.org
MIT License
18.63k stars 607 forks source link

fix(types): add undefined initial value to Atom definition #2668

Closed rtritto closed 3 months ago

rtritto commented 3 months ago

Related Bug Reports or Discussions

Discussion #2667

Fixes #2666

Check List

vercel[bot] commented 3 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 23, 2024 9:09am
codesandbox-ci[bot] commented 3 months ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

rtritto commented 3 months ago

Does it also need a change like this?

// write-only derived atom
export function atom<Value, Args extends unknown[], Result>(
-  initialValue: Value,
+  initialValue?: Value,
  write: Write<Args, Result>,
-): WritableAtom<Value, Args, Result> & WithInitialValue<Value>
+): WritableAtom<Value | undefined, Args, Result> & WithInitialValue<Value | undefined>
dai-shi commented 3 months ago

No, I think it should be explicit, and also TS doesn't allow it.

rtritto commented 3 months ago

Done

github-actions[bot] commented 3 months ago

LiveCodes Preview in LiveCodes

Latest commit: bd1348ff9002db031103c3b40ee6bf8a4d136330
Last updated: Jul 23, 2024 9:08am (UTC)

Playground Link
React demo https://livecodes.io?x=id/2YMSGQ3UN

See documentations for usage instructions.

rtritto commented 3 months ago

Done

dai-shi commented 3 months ago

@rtritto A friendly reminder

rtritto commented 3 months ago

@dai-shi I need some help

dai-shi commented 3 months ago

Sure, but for which one? Let's first focus on the first one.

rtritto commented 3 months ago

It's related to vanilla/types.test

dai-shi commented 3 months ago

Please add this and if you have more ideas, please add them.

diff --git a/tests/vanilla/types.test.tsx b/tests/vanilla/types.test.tsx
index e0744b8..3c89725 100644
--- a/tests/vanilla/types.test.tsx
+++ b/tests/vanilla/types.test.tsx
@@ -1,4 +1,5 @@
 import { expectType } from 'ts-expect'
+import type { TypeOf } from 'ts-expect'
 import { it } from 'vitest'
 import { atom } from 'jotai/vanilla'
 import type {
@@ -15,6 +16,15 @@ it('atom() should return the correct types', () => {
     // primitive atom
     const primitiveAtom = atom(0)
     expectType<PrimitiveAtom<number>>(primitiveAtom)
+    expectType<TypeOf<PrimitiveAtom<number>, typeof primitiveAtom>>(true)
+    expectType<TypeOf<PrimitiveAtom<number | undefined>, typeof primitiveAtom>>(
+      false,
+    )
+
+    // primitive atom without initial value
+    const primitiveWithoutInitialAtom = atom<number | undefined>()
+    expectType<PrimitiveAtom<number | undefined>>(primitiveWithoutInitialAtom)
+    expectType<PrimitiveAtom<undefined>>(atom())

     // read-only derived atom
     const readonlyDerivedAtom = atom((get) => get(primitiveAtom) * 2)
rtritto commented 3 months ago

Done (no other ideas)

dai-shi commented 3 months ago

Size Change: 0 B

Total Size: 87 kB

ℹ️ View Unchanged | Filename | Size | | :--- | :---: | | `./dist/babel/plugin-debug-label.js` | 932 B | | `./dist/babel/plugin-react-refresh.js` | 1.14 kB | | `./dist/babel/preset.js` | 1.41 kB | | `./dist/esm/babel/plugin-debug-label.mjs` | 1 kB | | `./dist/esm/babel/plugin-react-refresh.mjs` | 1.19 kB | | `./dist/esm/babel/preset.mjs` | 1.49 kB | | `./dist/esm/index.mjs` | 62 B | | `./dist/esm/react.mjs` | 1.02 kB | | `./dist/esm/react/utils.mjs` | 746 B | | `./dist/esm/utils.mjs` | 67 B | | `./dist/esm/vanilla.mjs` | 3.83 kB | | `./dist/esm/vanilla/utils.mjs` | 4.95 kB | | `./dist/index.js` | 242 B | | `./dist/react.js` | 1.06 kB | | `./dist/react/utils.js` | 1.39 kB | | `./dist/system/babel/plugin-debug-label.development.js` | 1.1 kB | | `./dist/system/babel/plugin-debug-label.production.js` | 775 B | | `./dist/system/babel/plugin-react-refresh.development.js` | 1.29 kB | | `./dist/system/babel/plugin-react-refresh.production.js` | 928 B | | `./dist/system/babel/preset.development.js` | 1.59 kB | | `./dist/system/babel/preset.production.js` | 1.14 kB | | `./dist/system/index.development.js` | 252 B | | `./dist/system/index.production.js` | 183 B | | `./dist/system/react.development.js` | 1.17 kB | | `./dist/system/react.production.js` | 715 B | | `./dist/system/react/utils.development.js` | 860 B | | `./dist/system/react/utils.production.js` | 462 B | | `./dist/system/utils.development.js` | 257 B | | `./dist/system/utils.production.js` | 187 B | | `./dist/system/vanilla.development.js` | 3.92 kB | | `./dist/system/vanilla.production.js` | 2.06 kB | | `./dist/system/vanilla/utils.development.js` | 5.16 kB | | `./dist/system/vanilla/utils.production.js` | 3.09 kB | | `./dist/umd/babel/plugin-debug-label.development.js` | 1.08 kB | | `./dist/umd/babel/plugin-debug-label.production.js` | 852 B | | `./dist/umd/babel/plugin-react-refresh.development.js` | 1.27 kB | | `./dist/umd/babel/plugin-react-refresh.production.js` | 1 kB | | `./dist/umd/babel/preset.development.js` | 1.54 kB | | `./dist/umd/babel/preset.production.js` | 1.22 kB | | `./dist/umd/index.development.js` | 383 B | | `./dist/umd/index.production.js` | 328 B | | `./dist/umd/react.development.js` | 1.18 kB | | `./dist/umd/react.production.js` | 786 B | | `./dist/umd/react/utils.development.js` | 1.53 kB | | `./dist/umd/react/utils.production.js` | 1.01 kB | | `./dist/umd/utils.development.js` | 399 B | | `./dist/umd/utils.production.js` | 342 B | | `./dist/umd/vanilla.development.js` | 4.91 kB | | `./dist/umd/vanilla.production.js` | 2.69 kB | | `./dist/umd/vanilla/utils.development.js` | 6.07 kB | | `./dist/umd/vanilla/utils.production.js` | 3.72 kB | | `./dist/utils.js` | 247 B | | `./dist/vanilla.js` | 4.81 kB | | `./dist/vanilla/utils.js` | 5.94 kB |

compressed-size-action