Skip to content
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

Closed
luky1984 opened this issue Dec 18, 2019 · 4 comments
Closed

Flatten Function Parameters and remember param names? #35752

luky1984 opened this issue Dec 18, 2019 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@luky1984
Copy link

luky1984 commented Dec 18, 2019

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)

type Flatten<T extends E[], E extends any[] = any[]> = {
  [K in keyof T]: T[K][Exclude<keyof T[K], keyof any[]>]
}

const a = (x: string) => { }

const c = (...args: Parameters<typeof a>) { }
const d = (...args: Flatten<[Parameters<typeof a>]) { } // later here can be Flatten<[Tuple1, Tuple2]> only one for now.

c() // arguments are: x:string
d() // argumentes are: args_0:string

Best would be something like this

const e = (...args: [...Parameters<typeof a>]) { }

https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=11&pc=4#code/C4TwDgpgBAYgNgQ2MCA7APAFShAHi1AEwGcoBRAbQF0AacnfNEqBVEaqAXhbeoD4uUAN4AoKFAoBpKAEtUUANYQQAewBmUTFQBcmqVQplcAYzgBXQhHRLVGzPro31PdlT5URAXxEjjK1MTALIIAFLi6gQBOcgDmAJRcAkJQ3r7+gVDGoQB0uQiRMcS6AAr5CAC2ECiRxOigkM4IfAnJ3n4BQYQ5eQVFsIjIaOgUpZEVVRA1deAQje4tKT7GIXEihCtAA

@jcalz
Copy link
Contributor

jcalz commented Dec 18, 2019

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!

Playground link

@luky1984
Copy link
Author

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.

@jcalz
Copy link
Contributor

jcalz commented Dec 18, 2019

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).

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Dec 18, 2019
@RyanCavanaugh
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants