-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add type-level
readonly()
api (#593)
* feat: add type-level `readonly()` api * Update src/reactivity/readonly.ts * chore: update tests
- Loading branch information
Showing
7 changed files
with
172 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { reactive, Ref, UnwrapRef } from '.' | ||
import { isArray, isPlainObject, warn } from '../utils' | ||
import { readonlySet } from '../utils/sets' | ||
|
||
export function isReadonly(obj: any): boolean { | ||
return readonlySet.has(obj) | ||
} | ||
|
||
type Primitive = string | number | boolean | bigint | symbol | undefined | null | ||
type Builtin = Primitive | Function | Date | Error | RegExp | ||
|
||
// prettier-ignore | ||
export type DeepReadonly<T> = T extends Builtin | ||
? T | ||
: T extends Map<infer K, infer V> | ||
? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> | ||
: T extends ReadonlyMap<infer K, infer V> | ||
? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> | ||
: T extends WeakMap<infer K, infer V> | ||
? WeakMap<DeepReadonly<K>, DeepReadonly<V>> | ||
: T extends Set<infer U> | ||
? ReadonlySet<DeepReadonly<U>> | ||
: T extends ReadonlySet<infer U> | ||
? ReadonlySet<DeepReadonly<U>> | ||
: T extends WeakSet<infer U> | ||
? WeakSet<DeepReadonly<U>> | ||
: T extends Promise<infer U> | ||
? Promise<DeepReadonly<U>> | ||
: T extends {} | ||
? { readonly [K in keyof T]: DeepReadonly<T[K]> } | ||
: Readonly<T> | ||
|
||
// only unwrap nested ref | ||
type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRef<T> | ||
|
||
/** | ||
* **In @vue/composition-api, `reactive` only provides type-level readonly check** | ||
* | ||
* Creates a readonly copy of the original object. Note the returned copy is not | ||
* made reactive, but `readonly` can be called on an already reactive object. | ||
*/ | ||
export function readonly<T extends object>( | ||
target: T | ||
): DeepReadonly<UnwrapNestedRefs<T>> { | ||
return target as any | ||
} | ||
|
||
export function shallowReadonly<T extends object>(obj: T): Readonly<T> | ||
export function shallowReadonly(obj: any): any { | ||
if (!(isPlainObject(obj) || isArray(obj)) || !Object.isExtensible(obj)) { | ||
return obj | ||
} | ||
|
||
const readonlyObj = {} | ||
|
||
const source = reactive({}) | ||
const ob = (source as any).__ob__ | ||
|
||
for (const key of Object.keys(obj)) { | ||
let val = obj[key] | ||
let getter: (() => any) | undefined | ||
let setter: ((x: any) => void) | undefined | ||
const property = Object.getOwnPropertyDescriptor(obj, key) | ||
if (property) { | ||
if (property.configurable === false) { | ||
continue | ||
} | ||
getter = property.get | ||
setter = property.set | ||
if ( | ||
(!getter || setter) /* not only have getter */ && | ||
arguments.length === 2 | ||
) { | ||
val = obj[key] | ||
} | ||
} | ||
|
||
Object.defineProperty(readonlyObj, key, { | ||
enumerable: true, | ||
configurable: true, | ||
get: function getterHandler() { | ||
const value = getter ? getter.call(obj) : val | ||
ob.dep.depend() | ||
return value | ||
}, | ||
set(v) { | ||
if (__DEV__) { | ||
warn(`Set operation on key "${key}" failed: target is readonly.`) | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
readonlySet.set(readonlyObj, true) | ||
|
||
return readonlyObj | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { expectType, readonly, ref } from './index' | ||
|
||
describe('readonly', () => { | ||
it('nested', () => { | ||
const r = readonly({ | ||
obj: { k: 'v' }, | ||
arr: [1, 2, '3'], | ||
objInArr: [{ foo: 'bar' }], | ||
}) | ||
|
||
// @ts-expect-error | ||
r.obj = {} | ||
// @ts-expect-error | ||
r.obj.k = 'x' | ||
|
||
// @ts-expect-error | ||
r.arr.push(42) | ||
// @ts-expect-error | ||
r.objInArr[0].foo = 'bar2' | ||
}) | ||
|
||
it('with ref', () => { | ||
const r = readonly( | ||
ref({ | ||
obj: { k: 'v' }, | ||
arr: [1, 2, '3'], | ||
objInArr: [{ foo: 'bar' }], | ||
}) | ||
) | ||
|
||
console.log(r.value) | ||
|
||
expectType<string>(r.value.obj.k) | ||
|
||
// @ts-expect-error | ||
r.value = {} | ||
|
||
// @ts-expect-error | ||
r.value.obj = {} | ||
// @ts-expect-error | ||
r.value.obj.k = 'x' | ||
|
||
// @ts-expect-error | ||
r.value.arr.push(42) | ||
// @ts-expect-error | ||
r.value.objInArr[0].foo = 'bar2' | ||
}) | ||
}) |