-
Notifications
You must be signed in to change notification settings - Fork 64
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
Question: Partial does not know how many arguments are available? #412
Comments
I tried a bit but it seems it's easy to get (require TS 3.0) // from https://github.com/Microsoft/TypeScript/pull/24897#issuecomment-400989548
type Tail<L extends any[]> = ((...x: L) => void) extends ((
h: any,
...rest: infer T
) => void)
? T
: never;
// R.partial
type Shift<T extends any[], U extends any[]> = {
0: T;
1: Shift<Tail<T>, Tail<U>>;
2: never; //=> how to throw type error?
}[U extends [] ? 0 : T[0] extends U[0] ? 1 : 2];
declare function partial<T extends any[], U extends any[], R>(
fn: (...args: T) => R,
args: U
): (...args: Shift<T, U>) => R;
// test
type ShiftTest = Shift<[1, 2, 3], [1, 2]>; //=> [3]
declare function tuple<T extends any[]>(...args: T): T;
declare function foo(x: null, y: boolean, z: number): string;
const a = partial(foo, tuple()); //=> (null, boolean, number) => string
const b = partial(foo, tuple(null)); //=> (boolean, number) => string
const c = partial(foo, tuple(null, false)); //=> (number) => string
const d = partial(foo, tuple(null, false, 0)); //=> () => string
const invalid = partial(foo, tuple(1)); //=> (...never) => string cc @tycho01 |
My Playground crashes once I paste in
Maybe with some It's great to see tuple manipulation has suddenly started going mainstream too -- progress! :) It'd be cool if we could otherwise constrain Another attempt:
I tried making a tuple type based on a
Alas though, this wasn't as useful as I hoped: it didn't provide free casts as a solution to
In this particular case though, couldn't we just change |
Given the following super simple snippet:
I would expect that
y
is a function that takes no parameter because one is already baked in. Of course same goes for other parameters, e.g. if the function takes 3 args and I partial 2, it should come out with 1 arg after the partial.Any information about this?
As far as I could see in the types, there is no overload for defining the parameter types as well. Is it possible to infer those?
The text was updated successfully, but these errors were encountered: