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

optimize: function type judgment #2457

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ type CreateStoreImpl = <
initializer: StateCreator<T, [], Mos>,
) => Mutate<StoreApi<T>, Mos>

const isFunction = (arg: unknown): arg is Function => {
return typeof arg === 'function'
}

const createStoreImpl: CreateStoreImpl = (createState) => {
type TState = ReturnType<typeof createState>
type Listener = (state: TState, prevState: TState) => void
Expand All @@ -67,10 +71,7 @@ const createStoreImpl: CreateStoreImpl = (createState) => {
const setState: StoreApi<TState>['setState'] = (partial, replace) => {
// TODO: Remove type assertion once https://github.com/microsoft/TypeScript/issues/37663 is resolved
// https://github.com/microsoft/TypeScript/issues/37663#issuecomment-759728342
const nextState =
typeof partial === 'function'
? (partial as (state: TState) => TState)(state)
: partial
const nextState = isFunction(partial) ? partial(state) : partial
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's our intention to avoid this. We hope TS fixes it in the future.

if (!Object.is(nextState, state)) {
const previousState = state
state =
Expand Down