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

refactor(general): use notPrimitive to simplify both isObject and isFunction #12694

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions packages/runtime-core/src/apiAsyncComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
currentInstance,
isInSSRComponentSetup,
} from './component'
import { isFunction, isObject } from '@vue/shared'
import { isFunction, notPrimitive } from '@vue/shared'
import type { ComponentPublicInstance } from './componentPublicInstance'
import { type VNode, createVNode } from './vnode'
import { defineComponent } from './apiDefineComponent'
Expand Down Expand Up @@ -106,7 +106,7 @@ export function defineAsyncComponent<
) {
comp = comp.default
}
if (__DEV__ && comp && !isObject(comp) && !isFunction(comp)) {
if (__DEV__ && comp && !notPrimitive(comp)) {
throw new Error(`Invalid async component load result: ${comp}`)
}
resolvedComp = comp
Expand Down
9 changes: 6 additions & 3 deletions packages/shared/src/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ export const isString = (val: unknown): val is string => typeof val === 'string'
export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
export const isObject = (val: unknown): val is Record<any, any> =>
val !== null && typeof val === 'object'
export const notPrimitive = (
val: unknown,
): val is Record<any, any> | Function => Object(val) === val

export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
return (
(isObject(val) || isFunction(val)) &&
isFunction((val as any).then) &&
isFunction((val as any).catch)
notPrimitive(val) &&
isFunction((val as PromiseLike<T>).then) &&
isFunction((val as Promise<T>).catch)
)
}

Expand Down