-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type JS's arguments
object based on function parameters
#29055
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
Comments
What about using rest arguments? The following typechecks: class A {
fn(it: number) { return true; }
}
class B extends A {
fn(...args: [number]) {
// this shouldn't error, because arguments must be at least length 1.
// instead we get ERROR: Expected 1 arguments, but got 0 or more.
return super.fn(...args);
}
} |
Rest arguments work, but:
For example, my real subclass's function signature looked more like: fn(
data: PartialDataProps<X> | PartialDataProps<X>[] = {},
options: Parameters<typeof A["fn"]>[1] = {}
) { return true; } Holding aside the default values, having the arguments named fn(...args: [
PartialDataProps<X> | PartialDataProps<X>[],
Parameters<typeof A["fn"]>[1]
]) { return true; } |
Also, I just noticed that the |
The strictBindCallApply feature makes the following show an error for passing function one(a: string, b: number) {
}
function two(a: string, b: number) {
one.apply(null, arguments);
} |
Same problem here, I tried this but it didn't work either ...
testFn(a, b, c) {
const args = [...arguments].slice(0, 3);
testFn(...args);
}
... |
This bit me again today, where I had a function like: function wrapper(a: A, b: B, c: C, d: D, e: E) {
// callthrough to the wrapped function, which should have same signature
if(!someCondition) {
return wrapped(...arguments);
} else {
// do something clever
}
} Using rest parameters for one or both functions would've made their signatures basically unreadable. And every way I could think of to type-safely maintain the type of the arguments list in only one place (and then ig cast This example is similar to @chrmarti's, although with |
I've also tried to work around this by manually casting arguments to function wrapper() {
// Errror: Type alias 'Args' circularly references itself.
type Args = Parameters<typeof wrapper>;
return wrapped(...(arguments as Args));
} |
@ethanresnick For complex signatures, it will currently hit: #43731 |
This now works: type ToArrayLike<T extends unknown[]> = { [K in (keyof T) & `${bigint}`]: T[K] }
type Arguments<Fn extends (...args: any) => any> = IArguments & ToArrayLike<Parameters<Fn>>;
function fn(a: number, b: string) {
let args = arguments as Arguments<typeof fn>;
let _a = args[0];
// ^?
} (it's be great if TS could automatically infer |
Search Terms
type "arguments" object parameters
Suggestion
Within a function
x
, the type of thearguments
object should beParameters<typeof x> & IArguments
, withundefined
removed from the type of any entries inParameters<typeof x>
if that parameter has a default value.Use Cases
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: