Skip to content

Commit 5673363

Browse files
committed
feat: expose set/del as named exports
1 parent 45b932b commit 5673363

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/core/observer/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ export function defineReactive(
225225
* triggers change notification if the property doesn't
226226
* already exist.
227227
*/
228+
export function set<T>(array: T[], key: number, value: T): T
229+
export function set<T>(object: object, key: string | number, value: T): T
228230
export function set(
229-
target: Array<any> | Record<string, any>,
231+
target: any[] | Record<string, any>,
230232
key: any,
231233
val: any
232234
): any {
@@ -279,7 +281,9 @@ export function set(
279281
/**
280282
* Delete a property and trigger change if necessary.
281283
*/
282-
export function del(target: Array<any> | Object, key: any) {
284+
export function del<T>(array: T[], key: number): void
285+
export function del(object: object, key: string | number): void
286+
export function del(target: any[] | object, key: any) {
283287
if (__DEV__ && (isUndef(target) || isPrimitive(target))) {
284288
warn(
285289
`Cannot delete reactive property on undefined, null, or primitive value: ${target}`

src/v3/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,6 @@ export { h } from './h'
7272
export { getCurrentInstance } from './currentInstance'
7373
export { useSlots, useAttrs } from './apiSetup'
7474
export { nextTick } from 'core/util/next-tick'
75+
export { set, del } from 'core/observer'
7576

7677
export * from './apiLifecycle'

types/test/v3/reactivity-test.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
shallowReactive,
1212
readonly,
1313
markRaw,
14-
shallowReadonly
14+
shallowReadonly,
15+
set,
16+
del
1517
} from '../../index'
1618
import { describe, expectType } from '../utils'
1719

@@ -371,3 +373,15 @@ describe('shallowReadonly ref unwrap', () => {
371373
expectType<Ref>(r.count.n)
372374
r.count.n.value = 123
373375
})
376+
377+
describe('set/del', () => {
378+
set({}, 1, 'hi')
379+
set([], 1, 'bye')
380+
del({}, 'foo')
381+
del([], 1)
382+
383+
// @ts-expect-error
384+
set({}, 1)
385+
// @ts-expect-error
386+
del([], 'fse', 123)
387+
})

0 commit comments

Comments
 (0)