fix(types): add undefined initial value to Atom definition - #2668
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
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. |
| export function atom<Value = undefined>( | ||
| read: Read<Value | undefined>, | ||
| ): Atom<Value | undefined> |
There was a problem hiding this comment.
Hmm, I was expecting something like this:
| export function atom<Value = undefined>( | |
| read: Read<Value | undefined>, | |
| ): Atom<Value | undefined> | |
| export function atom<Value>(): PrimitiveAtom<Value | undefined> |
There was a problem hiding this comment.
This works but, in /src/vanilla/atom.ts, I get
This overload signature is not compatible with its implementation signature.ts(2394)
atom.ts(92, 17): The implementation signature is declared here.The signature (parameter) needs to be added.
There was a problem hiding this comment.
Yeah, let's do that. Just make it optional?
There was a problem hiding this comment.
Done, I used the primitive definition
|
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> |
|
No, I think it should be explicit, and also TS doesn't allow it. |
| // primitive atom | ||
| export function atom<Value>( | ||
| initialValue: Value, | ||
| ): PrimitiveAtom<Value> & WithInitialValue<Value> | ||
| initialValue?: Value, | ||
| ): PrimitiveAtom<Value | undefined> & WithInitialValue<Value | undefined> |
There was a problem hiding this comment.
No, it's looks bad. We need overloads.
// primitive atom without default value
export function atom<Value>(): PrimitiveAtom<Value | undefined> & WithInitialValue<Value | undefined>
// primitive atom
export function atom<Value>(
initialValue: Value,
): PrimitiveAtom<Value> & WithInitialValue<Value>
dai-shi
left a comment
There was a problem hiding this comment.
Sorry if it's confusing, but do not change existing public types. Only add a new overload type and possibly fix our type in the implementation (which is not public).
| // Do not depend on it as it can change without notice. | ||
| type WithInitialValue<Value> = { | ||
| init: Value | ||
| init: Value | undefined |
| // primitive atom without default value | ||
| export function atom<Value>(): PrimitiveAtom<Value> & WithInitialValue<Value> | ||
|
|
There was a problem hiding this comment.
Just for preference, I would like to have it before // primitive atom.
|
Done |
|
| Playground | Link |
|---|---|
| React demo | https://livecodes.io?x=id/2YMSGQ3UN |
See documentations for usage instructions.
dai-shi
left a comment
There was a problem hiding this comment.
The code looks good to me.
Can you add some tests?
|
Done |
|
|
||
| it('creates atoms', () => { | ||
| // primitive atom without initial value | ||
| const undefinedAtom = atom() |
There was a problem hiding this comment.
I'm sorry, but I didn't mean this test.
Please revert it and add "type" tests in tests/vanilla/types.test.tsx.
|
@rtritto A friendly reminder
|
|
@dai-shi I need some help |
|
Sure, but for which one? Let's first focus on the first one. |
|
It's related to |
|
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) |
|
Done (no other ideas) |
dai-shi
left a comment
There was a problem hiding this comment.
LGTM
Thanks for your contribution.
|
Size Change: 0 B Total Size: 87 kB ℹ️ View Unchanged
|
Related Bug Reports or Discussions
Discussion #2667
Fixes #2666
Check List
pnpm run prettierfor formatting code and docs