Here i have a type that takes a tuple type with a fixed number of parameters, and also includes an array of strings as the final parameter. I want to give the tuple type parameters custom names a, b instead of the default args_0, args_1, etc. Additionally, I want to give name c for new types added to tuple types:

type FParams = [a: string, b: number | undefined];

type Func = (...args: [...FParams, string[]]) => void;

const func: Func = (a, b) => undefined;

func('foo', undefined, ['bar']);

As a result of the above code pushing type cussing the argument names to be lost:

const func: (args_0: string, args_1: number | undefined, args_2: string[]) => void

However, I would like to preserve the type names and add an additional name c like this:

const func: (a: string, b: number | undefined, c: string[]) => void

Is there a way to achieve this?

1

There are 1 best solutions below

0
jcalz On BEST ANSWER

Yes, you can combine labeled tuples so that the labels are preserved:

type Func = (...args: [...x: FParams, c: string[]]) => void;
// dummy name ----------> ^^          ^^ <-- name you care about

// type Func = (a: string, b: number | undefined, c: string[]) => void

It would be nice if you could write [...FParams, c: string[]], but the rule is currently that tuple elements must either be all unlabeled like [...FParams, string[]] or all labeled like [...x: FParams, c: string[]]. So in order to get that c label in there you're forced to give a useless label to the spread FParams. There's an open feature request at microsoft/TypeScript#52853 to relax that restriction. But for now this is how you have to do it.

Playground link to code