pmndrs / jotai

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

jotai 1.6.4+ does not support typescript 4.0 #1398

Closed JLarky closed 2 years ago

JLarky commented 2 years ago

What happens: useAtom returns the value with type any. Why: declare type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T; this recursive declaration from useAtom is not supported in ts 4.0 What I have tested: I have ts 4.0 project and I noticed that it stopped building with jotai 1.7 and 1.8 but works in 1.6 I also tested jotai 1.8 on different versions using typescript playground and found that it works in 4.1 and up and doesn't work (i.e. returns any) in 4.0 and down

TS playground link: https://www.typescriptlang.org/play?ts=4.0.5#code/JYWwDg9gTgLgBAbzgQxhEAaOBXAzgUwEE0Q4BfOAMynTgHIArCGZYOgbgChOBjCAO1zwaQ-FADCyHgAt8cALwoSACjp0AlF14ChOAsXQBGaACYFcZeoUA+RJzhw+g+AG0AblkoBdc3iIqRGDFJGXx1ezgAekiHWIA9AH4ItwA6NAAZCAB3YOQCS04yTiA

dai-shi commented 2 years ago

Thanks for reporting. Does v1.6.4 break? https://github.com/pmndrs/jotai/blob/v1.6.4/src/core/useAtomValue.ts#L14 has the recursive definition.

dai-shi commented 2 years ago

https://ci.codesandbox.io/status/pmndrs/jotai/pr/1399 Try "Local Install Instructions" ☝️

JLarky commented 2 years ago

https://ci.codesandbox.io/status/pmndrs/jotai/pr/1399 Try "Local Install Instructions" ☝️

I didn't test it thoroughly but it doesn't seem like it fixed it

-jotai@^1.6.7:                                                                                                                                                
-  version "1.6.7"                                                                                                                                            
-  resolved "https://registry.yarnpkg.com/jotai/-/jotai-1.6.7.tgz#d5bdc747b66e369f1d1e859dad8bcda85acb946f"                                                   
-  integrity sha512-MxAm86FYHm6asU3O1hbTKOWpPMP5oI8zNlgFRpZBB85cX79psF2eRb7ZqdGewOgYUUUKAlzZ+sH0IM7Pea6MkA==                                                  
+"jotai@https://pkg.csb.dev/pmndrs/jotai/commit/d1038b1a/jotai":                                                                                              
+  version "1.8.2"                                                                                                                                            
+  resolved "https://pkg.csb.dev/pmndrs/jotai/commit/d1038b1a/jotai#c3fdb1f7abd48f44dd29d1b39ff5975cbce57657"
JLarky commented 2 years ago

Thanks for reporting. Does v1.6.4 break? https://github.com/pmndrs/jotai/blob/v1.6.4/src/core/useAtomValue.ts#L14 has the recursive definition.

yes, I tested this, on ts 4.0 I see 1.6.3 works fine, and 1.6.4 fails

dai-shi commented 2 years ago

I just tested locally with CRA, and it seems to work fine.

jotai-issue-1398.zip

JLarky commented 2 years ago

yeah, it works for me in some other simpler configurations, but I have monorepo with multiple typescript versions and I'm using microbundle. So it would be hard to make a proper reproduction. We can probably call it "fixed" for a general case though :)

dai-shi commented 2 years ago

yeah, sounds like your env doesn't handle "typeVersions" well. I'll add a commit in #1399, which may ease a workaround on your env.

dai-shi commented 2 years ago

Yay, it's working. jotai-issue-1398-2.zip

JLarky commented 2 years ago

yeah, sounds like your env doesn't handle "typeVersions" well. I'll add a commit in #1399, which may ease a workaround on your env.

Sorry for the confusion. I looked a bit more into it. Both 4.1 fix and 4.5 fix actually worked but I was getting some random error that I mistook for this original useAtom error. So I can confirm that both fixes actually work in ts 4.0. (I might look more into that other error as a separate issue ;) so...)

But I agree that using built-in Awaited is of course a better way moving forward :)