pmndrs / jotai

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

Store type is not exported for createStore() #2522

Closed AriaFallah closed 6 months ago

AriaFallah commented 6 months ago

Summary

Jotai doesn't export the Store type for createStore

Link to reproduction

const store: ??? = createStore()

Workaround

const store: ReturnType<typeof createStore> = createStore()

Question

Is there a reason why it's not exported?

dai-shi commented 6 months ago

We generally consider some types are internal, and don't export many types. In the case of Store, we didn't even have it internally either. I believe ReturnType<typeof createStore> is the right solution, not just a workaround, unless there's drawback. (I don't think I get "slow types" with JSR #2443, so it should be fine.)

AriaFallah commented 6 months ago

Hmm @dai-shi I'm not sure I fully understand. All return types on public functions are already part of the public API. By not exposing them, you put users in a weird spot where they cannot use the types they're already consuming in one way or another.

While it's true that you can do ReturnType<typeof createStore>, this really is a gigantic hack and a quirk of TypeScript's turing-complete type system. I don't think any non gradually typed programming languages would let you get away with returning a non-public type.