【发布时间】:2020-02-21 18:39:41
【问题描述】:
我正在尝试创建一个更新函数来更新记录中的深层值。我为不同深度的路径重载了变体。
我似乎无法弄清楚如何正确键入用于要更新的值的回调函数。
interface Test {
foo?: { bar: number }
}
const input: Test = { foo: { bar: 1 } }
update(input, 'foo', 'bar')(v => v + 1)
当我使用该函数时,它告诉我“Object(v) 的类型未知”。
但例如我有类似的 set 函数,它的定义几乎相同,但是当这样使用时它会正确输入:
set(input, 'foo', 'bar')(2)
这是我的功能
type UpdateFn<T> = (value: T) => T
export function update<T extends Record<string, any>, K1 extends keyof T>(
record: T | undefined,
key1: K1
): (callback: UpdateFn<NonNullable<T[K1]>>) => T
export function update<
T extends Record<string, any>,
K1 extends keyof T,
K2 extends keyof NonNullable<T[K1]>
>(
record: T | undefined,
key1: K1,
key2: K2
): (callback: UpdateFn<NonNullable<T[K1][K2]>>) => T
export function update<
T extends Record<string, any>,
K1 extends keyof T,
K2 extends keyof NonNullable<T[K1]>
>(
record: T | undefined,
key1: K1,
key2?: K2
): (
callback:
| UpdateFn<NonNullable<T[K1]>>
| UpdateFn<NonNullable<T[K1][K2]>>
) => T | undefined {
return callback => {
if (record === undefined) return record
if (key2 === undefined) {
const value = get(record, key1)
if (value === undefined) return record
return set(record, key1)(callback(value))
} else {
const value = get(record, key1, key2)
if (value === undefined) return record
return set(record, key1, key2)(callback(value))
}
}
}
设置(正常工作):
export function set<
T extends Record<string, any>,
K1 extends keyof T,
K2 extends keyof NonNullable<T[K1]>
>(record: T | undefined, key1: K1, key2: K2): (value: T[K1][K2]) => T
【问题讨论】:
-
你真的需要深度吗?例如你不能把
update(input, 'foo', 'bar')(v => v + 1)减少到update(input.foo, 'bar')(v => v + 1)吗? -
是的,我需要深度,因为函数需要返回整个更新的输入对象,只是 input.foo。否则它不容易组合。
标签: typescript typescript-generics