-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Flatten Function Parameters and remember param names? #35752
Comments
Questions like "how can I do this" do not belong here; try Stack Overflow instead. If you're requesting a feature or filing a bug then there are templates you should fill out, including some fields showing that you've done some of the initial research finding any existing issues that relate to your bug/request. Tuples inferred from rest parameters, as implemented in #24897, will hold onto the parameter names as some metadata associated with the tuple type itself. Once you pull elements out of the tuple and re-wrap into a new tuple, you've thrown away the original tuple type and the metadata with it: function f(x: string) { }
type ForgetParamName<T extends (arg: any) => any> = (...blah: [Parameters<T>[0]]) => ReturnType<T>;
type ForgottenF = ForgetParamName<typeof f>;
// type ForgottenF = (blah_0: string) => void There are ways to manipulate tuple types without losing this metadata, like mapping over them: type Numberify<T extends any[]> = Extract<{ [K in keyof T]: number }, any[]>;
type ToNumberParams<T extends (...args: any) => any> =
(...args: Numberify<Parameters<T>>) => ReturnType<T>
type RememberedF = ToNumberParams<typeof f>;
// type RememberedF = (...x: number) => void or converting back and forth to function parameter lists: type PrependParam<T extends (...args: any) => any> =
(addedParam: number, ...args: Parameters<T>) => ReturnType<T>
type AlsoRememberedF = PrependParam<typeof f>;
// type AlsoRememberedF = (addedParam: number, x: string) => void But that's about it... arbitrary manipulation of tuples is likely to lose this information. If you're asking about adding some feature to deal with this, you should fill out the template and try to locate existing related issues, such as maybe #28259. Cheers! |
Thank you its nice, but its not allowing to spread Tuple1 together with Tuple2 and make new params, only adding new single param. It is interesting still, but not fixing the problem. |
This isn't a forum for fixing problems... if you are making a feature suggestion, such as "concatenate tuple types while preserving parameter names", there is a template you should fill out, along with search terms and related issues. (Note, possibly related to #5453, which doesn't explicitly mention parameter names). |
To expedite the triage process, we need everyone to follow the issue template and instructions. When you clicked "Create New Issue", the issue form was pre-populated with a template and some instructions. We need you to read those instructions completely, follow them, and then fill in all the fields in that template. We are not able to assist with issues that don't follow the template instructions as they represent a significantly larger amount of work compared to issues which are correctly specified. Thank you for understanding. |
I would like to create new tuple of function params A and B.. that means flatten together two function params. I have the flatten type for that. For sake of simplicity showing only with one Parameters not two below.
But problem is that Typescript then forgets the parameter names, would it be possible to fix it somehow? Neither is possible to extract the params names from Parameters<> helper. (just saying another not related problem)
https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=11&pc=4#code/C4TwDgpgBAYgNgQ2MCA7APAFShAHi1AEwGcoBRAbQF0AacnfNEqBVEaqAXhbeoD4uUAN4AoKFAoBpKAEtUUANYQQAewBmUTFQBcmqVQplcAYzgBXQhHRLVGzPro31PdlT5URAXxEjjK1MTALIIAFLi6gQBOcgDmAJRcAkJQ3r7+gVDGoQB0uQiRMcS6AAr5CAC2ECiRxOigkM4IfAnJ3n4BQYQ5eQVFsIjIaOgUpZEVVRA1deAQje4tKT7GIXEihCtAA
The text was updated successfully, but these errors were encountered: