英文:
Typescript, How to constrain types according to function parameters?
问题
如何限制T以符合键的要求。
英文:
What can be done to make the useWatchProps callback function parameters correspond to the first parameter
type KeysToValues<O extends Record<string, any>, K> = K extends [
infer G,
...infer L
]
? G extends keyof O
? [O[G], ...KeysToValues<O, L>]
: []
: []
function useWatchProps<
O extends Record<string, any>,
K extends keyof O = keyof O,
T extends K | K[] = K | K[]
>(
keys: T,
callback: (values: T extends K ? O[K] : KeysToValues<O, K>) => void
) {}
useWatchProps<{ a: number; b: string }>('a', (a) => {
a // number
})
useWatchProps<{ a: number; b: string }>(['a', 'b'], ([a, b]) => {
a // number
b // string
})
How to constrain T by keys
答案1
得分: 0
以下是您要的翻译内容:
"Since you are not passing the T
, it gets automatically turned to the default value that you have assigned. Unfortunately, we can't specify only the portion of the required parameters; thus, I can suggest making useWatchProps
accept only O
and return another function. Usage will look like this:
useWatchProps<{ a: number; b: string }>()('a', (a) => {
a; // number
});
We will also need to change T
to a const type parameter to avoid compiler turning the T
to a union. This will cause to turn all checks of the T
to readonly some_array/some_tuple
.
Modifications:
type KeysToValues<O extends Record<string, any>, K> = K extends readonly [
infer G,
...infer L,
]
? G extends keyof O
? [O[G], ...KeysToValues<O, L>]
: []
: K
Implementation:
function useWatchProps<O extends Record<string, any>>() {
return <const T extends keyof O | readonly (keyof O)[]>(
keys: T,
callback: (values: T extends keyof O ? O[T] : KeysToValues<O, T>) => void,
) => {};
}
Usage:
useWatchProps<{ a: number; b: string }>()('a', (a) => {
a; // number
});
useWatchProps<{ a: number; b: string }>()(['a', 'b'], ([a, b]) => {
a; // number
b; // string
});
希望这对您有所帮助。
英文:
Since you are not passing the T
, it gets automatically turned to the default value that you have assigned. Unfortunately, we can't specify only the portion of the required parameters; thus, I can suggest making useWatchProps
accept only O
and return another function. Usage will look like this:
useWatchProps<{ a: number; b: string }>()('a', (a) => {
a; // number
});
We will also need to change T
to a const type parameter to avoid compiler turning the T
to a union. This will cause to turn all checks of the T
to readonly some_array/some_tuple
.
Modifications:
type KeysToValues<O extends Record<string, any>, K> = K extends readonly [
infer G,
...infer L,
]
? G extends keyof O
? [O[G], ...KeysToValues<O, L>]
: []
: K
Implementation:
function useWatchProps<O extends Record<string, any>>() {
return <const T extends keyof O | readonly (keyof O)[]>(
keys: T,
callback: (values: T extends keyof O ? O[T] : KeysToValues<O, T> ) => void,
) => {};
}
Usage:
useWatchProps<{ a: number; b: string }>()('a', (a) => {
a; // number
});
useWatchProps<{ a: number; b: string }>()(['a', 'b'] , ([a,b]) => {
a; // number
b; // string
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论