Skip to content
This repository was archived by the owner on Feb 16, 2021. It is now read-only.
This repository was archived by the owner on Feb 16, 2021. It is now read-only.

suggestion: dynamic function type #8

@amir-arad

Description

@amir-arad

Following gcanti/fp-ts#177,
It would be great if we had a variadic generic function type before microsoft/TypeScript#5453 is resolved.

My best shot was this:
(I've opened a separate PR #7 for NumberToString as it seems useful on its own)

export type NumberToString = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
export type Func<I extends number, A, R=void> = {
    0: ()=>R
    1: (a0:A[0]) => R
    2: (a0:A[0], a1:A[1]) => R
    3: (a0:A[0], a1:A[1], a2:A[2]) => R
    4: (a0:A[0], a1:A[1], a2:A[2], a3:A[3]) => R
    5: (a0:A[0], a1:A[1], a2:A[2], a3:A[3], a4:A[4]) => R
    6: (a0:A[0], a1:A[1], a2:A[2], a3:A[3], a4:A[4], a5:A[5]) => R
    7: (a0:A[0], a1:A[1], a2:A[2], a3:A[3], a4:A[4], a5:A[5], a6:A[6]) => R
    8: (a0:A[0], a1:A[1], a2:A[2], a3:A[3], a4:A[4], a5:A[5], a6:A[6], a7:A[7]) => R
    9: (a0:A[0], a1:A[1], a2:A[2], a3:A[3], a4:A[4], a5:A[5], a6:A[6], a7:A[7], a8:A[8]) => R
    10: (a0:A[0], a1:A[1], a2:A[2], a3:A[3], a4:A[4], a5:A[5], a6:A[6], a7:A[7], a8:A[8], a9:A[9]) => R
}[NumberToString[I]];

usage example:

const stringToNumber: Func<1, [string], number> = Number.parseInt; //works
const numberToNumber: Func<1, [number], number> = Number.parseInt; // breaks

However the first generic param, I seems redundant though I couldn't shake it off. I'm beginning to use this type in my own project, and I would welcome any improvement ideas.

@SimonMeskens

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions