在具有 pointfree omit
的情况下,我可以这样做:
type PlainObject<T> = {[key: string]: T}
const omit = <K extends string>(
name: K
) => <T, U extends PlainObject<T> & {
[P in K]: T
}>(
x: U,
): Partial<U> => {
throw new Error ('Yet to implement !')
}
它们是使用K
作为字符串而不是字符串数组的一种方法吗?
与此类似:
const omit = <K extends string[]>(
...names: K
) => <T, U extends PlainObject<T> & {
[P in K]: T // Type 'K' is not assignable to type 'string | number | symbol'
}>(
x: U,
): Partial<U> => {
throw new Error ('Yet to implement !')
}
目标是能够使编译器抱怨用户是否根据密钥传递输入了错误的对象:omit ('a', 'b') ({b: 3, c: 4}) // => expect error
预先感谢
塞巴
答案 0 :(得分:1)
是的,有可能:
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
declare function omit<K extends string>(...properties: K[]):
<T extends Record<K, any>>(source: T) => Omit<T, K>;
用法:
interface Foo {
name: string;
age: number;
brand: symbol;
}
declare const foo: Foo;
omit('age', 'name')(foo); // $ExpectType { brand: symbol }
omit('foo', 'bar')(foo); // $ExpectError