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
Spreading tuple into generic/type arguments #39648
Comments
one workaround could potentially be using an object (?) type instead? function foo<T extends { Foo: unknown; Bar: unknown; Baz: unknown; Quux: unknown; }>(t: T['Foo'], u: T['Baz']): [T['Foo'], T['Baz']] {
return [t, u];
}
type Foo = 1;
type Bar = 2;
type Baz = 3;
type Quux = 4;
type ThreeType = { Foo: Foo; Bar: Bar; Baz: Baz; };
foo<ThreeType & { Quux: Quux; }>(1, 3);
// ^? |
This would be particularly useful for interacting with libraries that use generics that take multiple arguments rather than an object type. A situation I find myself in. |
Find myself in the same issue, third party so can't move to object |
Running into this with Express - though I can say it might just be their Typings because I've never needed this before. I want to extend their Request and have to use something like this: export interface AuthedRequest<
P = ParamsDictionary,
ResBody = any,
ReqBody = any,
ReqQuery = ParsedQs,
LocalsObj extends Record<string, any> = Record<string, any>
> extends Request<P, ResBody, ReqBody, ReqQuery, LocalsObj> {
test: string
} |
same issue here! current code: type UpdateLabelData = Label;
type UpdateLabelError = unknown;
type UpdateLabelVariables = LabelArguments;
type UpdateLabelContext = { previousLabels: Label[] };
export function useUpdateLabel(
options?: UseMutationOptions<
UpdateLabelData,
UpdateLabelError,
UpdateLabelVariables,
UpdateLabelContext
>,
) {
const mutation = useMutation<
UpdateLabelData,
UpdateLabelError,
UpdateLabelVariables,
UpdateLabelContext
>({ would love: type UpdateLabelGeneric = [Label, unknown, LabelArguments, { previousLabels: Label[] }];
export function useUpdateLabel(
options?: UseMutationOptions<...UpdateLabelGeneric>,
) {
const mutation = useMutation<...UpdateLabelGeneric>({ |
This would be cool – I naïvely requested this feature in type-fest as syntactic sugar to allow the // before
type MyType = Merge<
FinalDestination,
Merge<
IntermediateDestination,
Source,
>,
>;
// After
type MyType = Merge<FinalDestination, IntermediateDestination, Source> |
@roryabraham nope - this kind is 100% possible. just:
and that's it (what this proposal would be adding is the ability to bundle up the array of parameters and pass the array around) |
Search Terms
Suggestion
Allow spreading of a tuple type into type arguments.
The idea is that a tuple of n elements gets spread into generic arguments with r arguments. If n is larger than r, only the first r items are used. If n is smaller than r it is possible to add elements after the spread argument. E.g.
Use Cases
I find myself repeating complex generic types. Ideally I could shorten lengthy repetition into a single type.
Examples
dialog.open
takes three generic arguments, these have to be repeated each call. This makes the call less legible and reduces DRYness.After this proposal
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: