Skip to content

Commit 2ba5a66

Browse files
authored
refactor: use utils isFunction (#768)
Co-authored-by: webfansplz <>
1 parent d467b8f commit 2ba5a66

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

src/apis/computed.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
noopFn,
66
defineComponentInstance,
77
getVueInternalClasses,
8+
isFunction,
89
} from '../utils'
910

1011
export interface ComputedRef<T = any> extends WritableComputedRef<T> {
@@ -35,7 +36,7 @@ export function computed<T>(
3536
let getter: ComputedGetter<T>
3637
let setter: ComputedSetter<T> | undefined
3738

38-
if (typeof getterOrOptions === 'function') {
39+
if (isFunction(getterOrOptions)) {
3940
getter = getterOrOptions
4041
} else {
4142
getter = getterOrOptions.get

src/apis/watch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ export function watch<T = any>(
432432
options?: WatchOptions
433433
): WatchStopHandle {
434434
let callback: WatchCallback<unknown> | null = null
435-
if (typeof cb === 'function') {
435+
if (isFunction(cb)) {
436436
// source watch
437437
callback = cb as WatchCallback<unknown>
438438
} else {

src/install.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { VueConstructor } from 'vue'
22
import { AnyObject } from './types/basic'
3-
import { hasSymbol, hasOwn, isPlainObject, warn } from './utils'
3+
import { isFunction, hasSymbol, hasOwn, isPlainObject, warn } from './utils'
44
import { isRef } from './reactivity'
55
import { setVueConstructor, isVueRegistered } from './runtimeContext'
66
import { mixin } from './mixin'
@@ -67,8 +67,8 @@ export function install(Vue: VueConstructor) {
6767
) {
6868
return function mergedSetupFn(props: any, context: any) {
6969
return mergeData(
70-
typeof parent === 'function' ? parent(props, context) || {} : undefined,
71-
typeof child === 'function' ? child(props, context) || {} : undefined
70+
isFunction(parent) ? parent(props, context) || {} : undefined,
71+
isFunction(child) ? child(props, context) || {} : undefined
7272
)
7373
}
7474
}

src/mixin.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function mixin(Vue: VueConstructor) {
6060
if (!setup) {
6161
return
6262
}
63-
if (typeof setup !== 'function') {
63+
if (!isFunction(setup)) {
6464
if (__DEV__) {
6565
warn(
6666
'The "setup" option should be a function that returns a object in component definitions.',
@@ -74,7 +74,7 @@ export function mixin(Vue: VueConstructor) {
7474
// wrapper the data option, so we can invoke setup before data get resolved
7575
$options.data = function wrappedData() {
7676
initSetup(vm, vm.$props)
77-
return typeof data === 'function'
77+
return isFunction(data)
7878
? (
7979
data as (this: ComponentInstance, x: ComponentInstance) => object
8080
).call(vm, vm)

src/runtimeContext.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { VueConstructor, VNode } from 'vue'
22
import { ComponentInstance, Data } from './component'
3-
import { assert, hasOwn, warn, proxy, UnionToIntersection } from './utils'
3+
import {
4+
assert,
5+
hasOwn,
6+
warn,
7+
proxy,
8+
UnionToIntersection,
9+
isFunction,
10+
} from './utils'
411

512
let vueDependency: VueConstructor | undefined = undefined
613

@@ -25,7 +32,7 @@ let currentInstance: ComponentInstance | null = null
2532
const PluginInstalledFlag = '__composition_api_installed__'
2633

2734
function isVue(obj: any): obj is VueConstructor {
28-
return obj && typeof obj === 'function' && obj.name === 'Vue'
35+
return obj && isFunction(obj) && obj.name === 'Vue'
2936
}
3037

3138
export function isPluginInstalled() {

0 commit comments

Comments
 (0)