Skip to content
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

fix(types): reactiveOmit and reactivePick support array as second arg #1742

Merged
merged 6 commits into from Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/shared/reactiveOmit/index.ts
@@ -1,5 +1,6 @@
import { toRefs } from 'vue-demi'
import { reactiveComputed } from '../reactiveComputed'
import type { Many } from '../utils'

/**
* Reactively omit fields from a reactive object
Expand All @@ -8,7 +9,7 @@ import { reactiveComputed } from '../reactiveComputed'
*/
export function reactiveOmit<T extends object, K extends keyof T>(
obj: T,
...keys: K[]
...keys: Array<Many<K>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
...keys: Array<Many<K>>
...keys: (K | K[])[]

The abstraction of Many make understanding it a bit harder. I think it's better to wrote them as literals

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Yes the literal meaning is really easier to understand

): Omit<T, K> {
return reactiveComputed<any>(() => Object.fromEntries(Object.entries(toRefs(obj)).filter(e => !keys.includes(e[0] as any))))
}
3 changes: 2 additions & 1 deletion packages/shared/reactivePick/index.ts
@@ -1,5 +1,6 @@
import type { UnwrapRef } from 'vue-demi'
import { reactive, toRef } from 'vue-demi'
import { Many } from '../utils'

/**
* Reactively pick fields from a reactive object
Expand All @@ -8,7 +9,7 @@ import { reactive, toRef } from 'vue-demi'
*/
export function reactivePick<T extends object, K extends keyof T>(
obj: T,
...keys: K[]
...keys: Array<Many<K>>
): { [S in K]: UnwrapRef<T[S]> } {
return reactive(Object.fromEntries(keys.map(k => [k, toRef(obj, k)]))) as any
}
2 changes: 2 additions & 0 deletions packages/shared/utils/types.ts
Expand Up @@ -51,6 +51,8 @@ export type ShallowUnwrapRef<T> = T extends Ref<infer P> ? P : T

export type Awaitable<T> = Promise<T> | T

export type Many<T> = ReadonlyArray<T> | T

export interface Pausable {
/**
* A ref indicate whether a pusable instance is active
Expand Down