On a scenario to have a pointfree omit i can do this:
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 !')
}
Is they're a way to use K as an array of string instead of a string?
Something along the line of this:
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 !')
}
The goal is to be able to have the compiler complains if a user enters a wrong object according to the key passes: omit ('a', 'b') ({b: 3, c: 4}) // => expect error
Thanks in advance
Seb
Kis an array of strings, then you want to map overK[number], notK. So{[P in K[number]]: T}should work for you, I guess. It's hard to be 100% sure because the code doesn't seem to be a minimal reproducible example (what isPlainObject<T>? why is there aTand aUin the returned function, whenTwill almost certainly not be inferred as anything but{}? what is the intended return type of the returned function?)Record<string, T>, and i'm using PlainObject to be able to extends it.Tcan be usefull since when an object is passed it's an object of number as values, those would be inferred without issue (I might be wrong, but based on the documentations i've red is whenever you can try to adds generics for better inference - it's not needed?) - as for U what I want is to have a PlainObject has a return object but containing some of the keys of the original object (in the implementation i put all the copied items in aPartial<U>K[number]) in order to better appreciates it? I see it use often on pieces of codes but still hasn't grasp itPartial Uis theyre a way of reconstructing precisely the object resulting from a call toomitusing the key of U (which we know) for keeping only the one that aren't removed? It would be a better experience that instead of optional (and possibly missing keys) the resulting object only actual keys ^^