Skip to content
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

Merged
merged 3 commits into from
Jul 23, 2024

Conversation

rtritto
Copy link
Contributor

@rtritto rtritto commented Jul 20, 2024

Related Bug Reports or Discussions

Discussion #2667

Fixes #2666

Check List

  • pnpm run prettier for formatting code and docs

Copy link

vercel bot commented Jul 20, 2024

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

Copy link

codesandbox-ci bot commented Jul 20, 2024

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.

Comment on lines 79 to 81
export function atom<Value = undefined>(
read: Read<Value | undefined>,
): Atom<Value | undefined>
Copy link
Member

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:

Suggested change
export function atom<Value = undefined>(
read: Read<Value | undefined>,
): Atom<Value | undefined>
export function atom<Value>(): PrimitiveAtom<Value | undefined>

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

@rtritto rtritto Jul 20, 2024

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

@rtritto
Copy link
Contributor Author

rtritto commented Jul 20, 2024

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
Copy link
Member

dai-shi commented Jul 20, 2024

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

Comment on lines 84 to 87
// primitive atom
export function atom<Value>(
initialValue: Value,
): PrimitiveAtom<Value> & WithInitialValue<Value>
initialValue?: Value,
): PrimitiveAtom<Value | undefined> & WithInitialValue<Value | undefined>
Copy link
Member

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>

rtritto

This comment was marked as duplicate.

Copy link
Member

@dai-shi dai-shi left a 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).

@@ -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
Copy link
Member

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.

Comment on lines 89 to 92
// primitive atom without default value
export function atom<Value>(): PrimitiveAtom<Value> & WithInitialValue<Value>

Copy link
Member

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.

@rtritto
Copy link
Contributor Author

rtritto commented Jul 20, 2024

Done

Copy link

github-actions bot commented Jul 20, 2024

LiveCodes Preview in LiveCodes

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

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

See documentations for usage instructions.

Copy link
Member

@dai-shi dai-shi left a 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?

@rtritto
Copy link
Contributor Author

rtritto commented Jul 20, 2024

Done

@@ -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()
Copy link
Member

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.

@dai-shi
Copy link
Member

dai-shi commented Jul 23, 2024

@rtritto A friendly reminder

@rtritto
Copy link
Contributor Author

rtritto commented Jul 23, 2024

@dai-shi I need some help

@dai-shi
Copy link
Member

dai-shi commented Jul 23, 2024

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

@rtritto
Copy link
Contributor Author

rtritto commented Jul 23, 2024

It's related to vanilla/types.test

@dai-shi
Copy link
Member

dai-shi commented Jul 23, 2024

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
Copy link
Contributor Author

rtritto commented Jul 23, 2024

Done (no other ideas)

Copy link
Member

@dai-shi dai-shi left a 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.

@dai-shi dai-shi added this to the v2.9.1 milestone Jul 23, 2024
@dai-shi
Copy link
Member

dai-shi commented Jul 23, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[TypeScript] Add undefined to Atom definition
2 participants