-
-
Notifications
You must be signed in to change notification settings - Fork 613
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
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. |
src/vanilla/atom.ts
Outdated
export function atom<Value = undefined>( | ||
read: Read<Value | undefined>, | ||
): Atom<Value | undefined> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, let's do that. Just make it optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, I used the primitive definition
65fd147
to
6f27425
Compare
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. |
src/vanilla/atom.ts
Outdated
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>
6f27425
to
57517fa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).
src/vanilla/atom.ts
Outdated
@@ -26,7 +26,7 @@ type Write<Args extends unknown[], Result> = ( | |||
// This is an internal type and not part of public API. | |||
// Do not depend on it as it can change without notice. | |||
type WithInitialValue<Value> = { | |||
init: Value | |||
init: Value | undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't do this.
src/vanilla/atom.ts
Outdated
// primitive atom without default value | ||
export function atom<Value>(): PrimitiveAtom<Value> & WithInitialValue<Value> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for preference, I would like to have it before // primitive atom
.
57517fa
to
d82bfc8
Compare
d82bfc8
to
e6ecdec
Compare
Done |
e6ecdec
to
499656c
Compare
499656c
to
aa956d7
Compare
aa956d7
to
ca397a7
Compare
Preview in LiveCodesLatest commit: bd1348f
See documentations for usage instructions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks good to me.
Can you add some tests?
ca397a7
to
2e71e7c
Compare
Done |
2e71e7c
to
e944186
Compare
tests/vanilla/basic.test.tsx
Outdated
@@ -2,6 +2,8 @@ import { expect, it } from 'vitest' | |||
import { atom } from 'jotai/vanilla' | |||
|
|||
it('creates atoms', () => { | |||
// primitive atom without initial value | |||
const undefinedAtom = atom() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
e944186
to
7f8cceb
Compare
Done (no other ideas) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 prettier
for formatting code and docs