Skip to content

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

Merged
dai-shi merged 3 commits into
pmndrs:mainfrom
rtritto:add-atom-undefined-init
Jul 23, 2024
Merged

fix(types): add undefined initial value to Atom definition#2668
dai-shi merged 3 commits into
pmndrs:mainfrom
rtritto:add-atom-undefined-init

Conversation

@rtritto

@rtritto rtritto commented Jul 20, 2024

Copy link
Copy Markdown
Contributor

Related Bug Reports or Discussions

Discussion #2667

Fixes #2666

Check List

  • pnpm run prettier for formatting code and docs

@vercel

vercel Bot commented Jul 20, 2024

Copy link
Copy Markdown

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

codesandbox-ci Bot commented Jul 20, 2024

Copy link
Copy Markdown

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 thread src/vanilla/atom.ts Outdated
Comment on lines +79 to +81
export function atom<Value = undefined>(
read: Read<Value | undefined>,
): Atom<Value | undefined>

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

@rtritto rtritto Jul 20, 2024

Copy link
Copy Markdown
Contributor Author

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

rtritto commented Jul 20, 2024

Copy link
Copy Markdown
Contributor Author

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

dai-shi commented Jul 20, 2024

Copy link
Copy Markdown
Member

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

Comment thread src/vanilla/atom.ts Outdated
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
Copy Markdown
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.

@dai-shi dai-shi left a comment

Copy link
Copy Markdown
Member

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).

Comment thread src/vanilla/atom.ts Outdated
// Do not depend on it as it can change without notice.
type WithInitialValue<Value> = {
init: Value
init: Value | undefined

Copy link
Copy Markdown
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 thread src/vanilla/atom.ts Outdated
Comment on lines +89 to +92
// primitive atom without default value
export function atom<Value>(): PrimitiveAtom<Value> & WithInitialValue<Value>

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

rtritto commented Jul 20, 2024

Copy link
Copy Markdown
Contributor Author

Done

@github-actions

github-actions Bot commented Jul 20, 2024

Copy link
Copy Markdown

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.

@dai-shi dai-shi left a comment

Copy link
Copy Markdown
Member

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

rtritto commented Jul 20, 2024

Copy link
Copy Markdown
Contributor Author

Done

Comment thread tests/vanilla/basic.test.tsx Outdated

it('creates atoms', () => {
// primitive atom without initial value
const undefinedAtom = atom()

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

dai-shi commented Jul 23, 2024

Copy link
Copy Markdown
Member

@rtritto A friendly reminder

@rtritto

rtritto commented Jul 23, 2024

Copy link
Copy Markdown
Contributor Author

@dai-shi I need some help

@dai-shi

dai-shi commented Jul 23, 2024

Copy link
Copy Markdown
Member

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

@rtritto

rtritto commented Jul 23, 2024

Copy link
Copy Markdown
Contributor Author

It's related to vanilla/types.test

@dai-shi

dai-shi commented Jul 23, 2024

Copy link
Copy Markdown
Member

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

rtritto commented Jul 23, 2024

Copy link
Copy Markdown
Contributor Author

Done (no other ideas)

@dai-shi dai-shi left a comment

Copy link
Copy Markdown
Member

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

dai-shi commented Jul 23, 2024

Copy link
Copy Markdown
Member

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