-
Notifications
You must be signed in to change notification settings - Fork 344
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
feat: add type-level readonly()
api
#593
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,101 @@ | ||
import { reactive, Ref, UnwrapRef } from '.' | ||
import { isArray, isPlainObject, warn } from '../utils' | ||
import { readonlySet } from '../utils/sets' | ||
import { isRef } from './ref' | ||
|
||
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>> { | ||
if (isRef(target)) { | ||
return target as any | ||
} | ||
return shallowReadonly(target) as any | ||
antfu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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,9 @@ | ||
import { readonly } from './index' | ||
|
||
describe('should support DeepReadonly', () => { | ||
const r = readonly({ obj: { k: 'v' } }) | ||
// @ts-expect-error | ||
r.obj = {} | ||
// @ts-expect-error | ||
r.obj.k = 'x' | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if is worth to have
readonly
if it's the same asshallowReadonly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's for the API alignment with Vue 3, lowing the difference between them. Thinking
import { readonly } from 'vue-demi
will work for Vue 3 but failed on Vue 2. This implementation acts like a noop but providing the type-level checking as the same as Vue 3. Should be good enough for users using TypeScript.