Skip to content

Commit

Permalink
feat: expose set/del as named exports
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 1, 2022
1 parent 45b932b commit 5673363
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/core/observer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ export function defineReactive(
* triggers change notification if the property doesn't
* already exist.
*/
export function set<T>(array: T[], key: number, value: T): T
export function set<T>(object: object, key: string | number, value: T): T
export function set(
target: Array<any> | Record<string, any>,
target: any[] | Record<string, any>,
key: any,
val: any
): any {
Expand Down Expand Up @@ -279,7 +281,9 @@ export function set(
/**
* Delete a property and trigger change if necessary.
*/
export function del(target: Array<any> | Object, key: any) {
export function del<T>(array: T[], key: number): void
export function del(object: object, key: string | number): void
export function del(target: any[] | object, key: any) {
if (__DEV__ && (isUndef(target) || isPrimitive(target))) {
warn(
`Cannot delete reactive property on undefined, null, or primitive value: ${target}`
Expand Down
1 change: 1 addition & 0 deletions src/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ export { h } from './h'
export { getCurrentInstance } from './currentInstance'
export { useSlots, useAttrs } from './apiSetup'
export { nextTick } from 'core/util/next-tick'
export { set, del } from 'core/observer'

export * from './apiLifecycle'
16 changes: 15 additions & 1 deletion types/test/v3/reactivity-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
shallowReactive,
readonly,
markRaw,
shallowReadonly
shallowReadonly,
set,
del
} from '../../index'
import { describe, expectType } from '../utils'

Expand Down Expand Up @@ -371,3 +373,15 @@ describe('shallowReadonly ref unwrap', () => {
expectType<Ref>(r.count.n)
r.count.n.value = 123
})

describe('set/del', () => {
set({}, 1, 'hi')
set([], 1, 'bye')
del({}, 'foo')
del([], 1)

// @ts-expect-error
set({}, 1)
// @ts-expect-error
del([], 'fse', 123)
})

0 comments on commit 5673363

Please sign in to comment.