From 2f32a8c6754db6206f055b08dda713cae79e190a Mon Sep 17 00:00:00 2001 From: Peter Sanderson Date: Tue, 3 Dec 2024 14:51:38 +0100 Subject: [PATCH 1/4] chore: split overloads types from utils.ts into solo-file --- packages/type-utils/src/index.ts | 1 + packages/type-utils/src/overloads.ts | 708 +++++++++++++++++++++++++++ packages/type-utils/src/utils.ts | 708 --------------------------- 3 files changed, 709 insertions(+), 708 deletions(-) create mode 100644 packages/type-utils/src/overloads.ts diff --git a/packages/type-utils/src/index.ts b/packages/type-utils/src/index.ts index 5f0d9cd5fd7..983b52309aa 100644 --- a/packages/type-utils/src/index.ts +++ b/packages/type-utils/src/index.ts @@ -1,2 +1,3 @@ export * from './utils'; export * from './timeout'; +export * from './overloads'; diff --git a/packages/type-utils/src/overloads.ts b/packages/type-utils/src/overloads.ts new file mode 100644 index 00000000000..ef5e952b917 --- /dev/null +++ b/packages/type-utils/src/overloads.ts @@ -0,0 +1,708 @@ +export type OverloadImplementations = Overloads; +export type Overloads = Overloads24; +type Overloads24 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; + (...args: infer A14): infer R14; + (...args: infer A15): infer R15; + (...args: infer A16): infer R16; + (...args: infer A17): infer R17; + (...args: infer A18): infer R18; + (...args: infer A19): infer R19; + (...args: infer A20): infer R20; + (...args: infer A21): infer R21; + (...args: infer A22): infer R22; + (...args: infer A23): infer R23; + (...args: infer A24): infer R24; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + (...args: A14) => R14, + (...args: A15) => R15, + (...args: A16) => R16, + (...args: A17) => R17, + (...args: A18) => R18, + (...args: A19) => R19, + (...args: A20) => R20, + (...args: A21) => R21, + (...args: A22) => R22, + (...args: A23) => R23, + (...args: A24) => R24, + ] + : Overloads23; +type Overloads23 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; + (...args: infer A14): infer R14; + (...args: infer A15): infer R15; + (...args: infer A16): infer R16; + (...args: infer A17): infer R17; + (...args: infer A18): infer R18; + (...args: infer A19): infer R19; + (...args: infer A20): infer R20; + (...args: infer A21): infer R21; + (...args: infer A22): infer R22; + (...args: infer A23): infer R23; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + (...args: A14) => R14, + (...args: A15) => R15, + (...args: A16) => R16, + (...args: A17) => R17, + (...args: A18) => R18, + (...args: A19) => R19, + (...args: A20) => R20, + (...args: A21) => R21, + (...args: A22) => R22, + (...args: A23) => R23, + ] + : Overloads22; +type Overloads22 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; + (...args: infer A14): infer R14; + (...args: infer A15): infer R15; + (...args: infer A16): infer R16; + (...args: infer A17): infer R17; + (...args: infer A18): infer R18; + (...args: infer A19): infer R19; + (...args: infer A20): infer R20; + (...args: infer A21): infer R21; + (...args: infer A22): infer R22; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + (...args: A14) => R14, + (...args: A15) => R15, + (...args: A16) => R16, + (...args: A17) => R17, + (...args: A18) => R18, + (...args: A19) => R19, + (...args: A20) => R20, + (...args: A21) => R21, + (...args: A22) => R22, + ] + : Overloads21; +type Overloads21 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; + (...args: infer A14): infer R14; + (...args: infer A15): infer R15; + (...args: infer A16): infer R16; + (...args: infer A17): infer R17; + (...args: infer A18): infer R18; + (...args: infer A19): infer R19; + (...args: infer A20): infer R20; + (...args: infer A21): infer R21; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + (...args: A14) => R14, + (...args: A15) => R15, + (...args: A16) => R16, + (...args: A17) => R17, + (...args: A18) => R18, + (...args: A19) => R19, + (...args: A20) => R20, + (...args: A21) => R21, + ] + : Overloads20; +type Overloads20 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; + (...args: infer A14): infer R14; + (...args: infer A15): infer R15; + (...args: infer A16): infer R16; + (...args: infer A17): infer R17; + (...args: infer A18): infer R18; + (...args: infer A19): infer R19; + (...args: infer A20): infer R20; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + (...args: A14) => R14, + (...args: A15) => R15, + (...args: A16) => R16, + (...args: A17) => R17, + (...args: A18) => R18, + (...args: A19) => R19, + (...args: A20) => R20, + ] + : Overloads19; +type Overloads19 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; + (...args: infer A14): infer R14; + (...args: infer A15): infer R15; + (...args: infer A16): infer R16; + (...args: infer A17): infer R17; + (...args: infer A18): infer R18; + (...args: infer A19): infer R19; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + (...args: A14) => R14, + (...args: A15) => R15, + (...args: A16) => R16, + (...args: A17) => R17, + (...args: A18) => R18, + (...args: A19) => R19, + ] + : Overloads18; +type Overloads18 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; + (...args: infer A14): infer R14; + (...args: infer A15): infer R15; + (...args: infer A16): infer R16; + (...args: infer A17): infer R17; + (...args: infer A18): infer R18; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + (...args: A14) => R14, + (...args: A15) => R15, + (...args: A16) => R16, + (...args: A17) => R17, + (...args: A18) => R18, + ] + : Overloads17; +type Overloads17 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; + (...args: infer A14): infer R14; + (...args: infer A15): infer R15; + (...args: infer A16): infer R16; + (...args: infer A17): infer R17; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + (...args: A14) => R14, + (...args: A15) => R15, + (...args: A16) => R16, + (...args: A17) => R17, + ] + : Overloads16; +type Overloads16 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; + (...args: infer A14): infer R14; + (...args: infer A15): infer R15; + (...args: infer A16): infer R16; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + (...args: A14) => R14, + (...args: A15) => R15, + (...args: A16) => R16, + ] + : Overloads15; +type Overloads15 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; + (...args: infer A14): infer R14; + (...args: infer A15): infer R15; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + (...args: A14) => R14, + (...args: A15) => R15, + ] + : Overloads14; +type Overloads14 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; + (...args: infer A14): infer R14; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + (...args: A14) => R14, + ] + : Overloads13; +type Overloads13 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; + (...args: infer A13): infer R13; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + (...args: A13) => R13, + ] + : Overloads12; +type Overloads12 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; + (...args: infer A12): infer R12; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + (...args: A12) => R12, + ] + : Overloads11; +type Overloads11 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; + (...args: infer A11): infer R11; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + (...args: A11) => R11, + ] + : Overloads10; +type Overloads10 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; + (...args: infer A10): infer R10; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + (...args: A10) => R10, + ] + : Overloads9; +type Overloads9 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; + (...args: infer A9): infer R9; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + (...args: A9) => R9, + ] + : Overloads8; +type Overloads8 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; + (...args: infer A8): infer R8; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + (...args: A8) => R8, + ] + : Overloads7; +type Overloads7 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; + (...args: infer A7): infer R7; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + (...args: A7) => R7, + ] + : Overloads6; +type Overloads6 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; + (...args: infer A6): infer R6; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + (...args: A6) => R6, + ] + : Overloads5; +type Overloads5 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; + (...args: infer A5): infer R5; +} + ? [ + (...args: A1) => R1, + (...args: A2) => R2, + (...args: A3) => R3, + (...args: A4) => R4, + (...args: A5) => R5, + ] + : Overloads4; +type Overloads4 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; + (...args: infer A4): infer R4; +} + ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4] + : Overloads3; +type Overloads3 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; + (...args: infer A3): infer R3; +} + ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3] + : Overloads2; +type Overloads2 = T extends { + (...args: infer A1): infer R1; + (...args: infer A2): infer R2; +} + ? [(...args: A1) => R1, (...args: A2) => R2] + : Overloads1; +type Overloads1 = T extends { + (...args: infer A1): infer R1; +} + ? [(...args: A1) => R1] + : unknown; diff --git a/packages/type-utils/src/utils.ts b/packages/type-utils/src/utils.ts index 11e814f1edb..c04847af02c 100644 --- a/packages/type-utils/src/utils.ts +++ b/packages/type-utils/src/utils.ts @@ -76,711 +76,3 @@ export type FilterPropertiesByType = { [Key in keyof T as T[Key] extends ValueFilter ? Key : never]: T[Key]; }; -export type OverloadImplementations = Overloads; -export type Overloads = Overloads24; -type Overloads24 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; - (...args: infer A14): infer R14; - (...args: infer A15): infer R15; - (...args: infer A16): infer R16; - (...args: infer A17): infer R17; - (...args: infer A18): infer R18; - (...args: infer A19): infer R19; - (...args: infer A20): infer R20; - (...args: infer A21): infer R21; - (...args: infer A22): infer R22; - (...args: infer A23): infer R23; - (...args: infer A24): infer R24; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - (...args: A14) => R14, - (...args: A15) => R15, - (...args: A16) => R16, - (...args: A17) => R17, - (...args: A18) => R18, - (...args: A19) => R19, - (...args: A20) => R20, - (...args: A21) => R21, - (...args: A22) => R22, - (...args: A23) => R23, - (...args: A24) => R24, - ] - : Overloads23; -type Overloads23 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; - (...args: infer A14): infer R14; - (...args: infer A15): infer R15; - (...args: infer A16): infer R16; - (...args: infer A17): infer R17; - (...args: infer A18): infer R18; - (...args: infer A19): infer R19; - (...args: infer A20): infer R20; - (...args: infer A21): infer R21; - (...args: infer A22): infer R22; - (...args: infer A23): infer R23; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - (...args: A14) => R14, - (...args: A15) => R15, - (...args: A16) => R16, - (...args: A17) => R17, - (...args: A18) => R18, - (...args: A19) => R19, - (...args: A20) => R20, - (...args: A21) => R21, - (...args: A22) => R22, - (...args: A23) => R23, - ] - : Overloads22; -type Overloads22 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; - (...args: infer A14): infer R14; - (...args: infer A15): infer R15; - (...args: infer A16): infer R16; - (...args: infer A17): infer R17; - (...args: infer A18): infer R18; - (...args: infer A19): infer R19; - (...args: infer A20): infer R20; - (...args: infer A21): infer R21; - (...args: infer A22): infer R22; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - (...args: A14) => R14, - (...args: A15) => R15, - (...args: A16) => R16, - (...args: A17) => R17, - (...args: A18) => R18, - (...args: A19) => R19, - (...args: A20) => R20, - (...args: A21) => R21, - (...args: A22) => R22, - ] - : Overloads21; -type Overloads21 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; - (...args: infer A14): infer R14; - (...args: infer A15): infer R15; - (...args: infer A16): infer R16; - (...args: infer A17): infer R17; - (...args: infer A18): infer R18; - (...args: infer A19): infer R19; - (...args: infer A20): infer R20; - (...args: infer A21): infer R21; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - (...args: A14) => R14, - (...args: A15) => R15, - (...args: A16) => R16, - (...args: A17) => R17, - (...args: A18) => R18, - (...args: A19) => R19, - (...args: A20) => R20, - (...args: A21) => R21, - ] - : Overloads20; -type Overloads20 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; - (...args: infer A14): infer R14; - (...args: infer A15): infer R15; - (...args: infer A16): infer R16; - (...args: infer A17): infer R17; - (...args: infer A18): infer R18; - (...args: infer A19): infer R19; - (...args: infer A20): infer R20; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - (...args: A14) => R14, - (...args: A15) => R15, - (...args: A16) => R16, - (...args: A17) => R17, - (...args: A18) => R18, - (...args: A19) => R19, - (...args: A20) => R20, - ] - : Overloads19; -type Overloads19 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; - (...args: infer A14): infer R14; - (...args: infer A15): infer R15; - (...args: infer A16): infer R16; - (...args: infer A17): infer R17; - (...args: infer A18): infer R18; - (...args: infer A19): infer R19; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - (...args: A14) => R14, - (...args: A15) => R15, - (...args: A16) => R16, - (...args: A17) => R17, - (...args: A18) => R18, - (...args: A19) => R19, - ] - : Overloads18; -type Overloads18 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; - (...args: infer A14): infer R14; - (...args: infer A15): infer R15; - (...args: infer A16): infer R16; - (...args: infer A17): infer R17; - (...args: infer A18): infer R18; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - (...args: A14) => R14, - (...args: A15) => R15, - (...args: A16) => R16, - (...args: A17) => R17, - (...args: A18) => R18, - ] - : Overloads17; -type Overloads17 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; - (...args: infer A14): infer R14; - (...args: infer A15): infer R15; - (...args: infer A16): infer R16; - (...args: infer A17): infer R17; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - (...args: A14) => R14, - (...args: A15) => R15, - (...args: A16) => R16, - (...args: A17) => R17, - ] - : Overloads16; -type Overloads16 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; - (...args: infer A14): infer R14; - (...args: infer A15): infer R15; - (...args: infer A16): infer R16; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - (...args: A14) => R14, - (...args: A15) => R15, - (...args: A16) => R16, - ] - : Overloads15; -type Overloads15 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; - (...args: infer A14): infer R14; - (...args: infer A15): infer R15; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - (...args: A14) => R14, - (...args: A15) => R15, - ] - : Overloads14; -type Overloads14 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; - (...args: infer A14): infer R14; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - (...args: A14) => R14, - ] - : Overloads13; -type Overloads13 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; - (...args: infer A13): infer R13; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - (...args: A13) => R13, - ] - : Overloads12; -type Overloads12 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; - (...args: infer A12): infer R12; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - (...args: A12) => R12, - ] - : Overloads11; -type Overloads11 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; - (...args: infer A11): infer R11; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - (...args: A11) => R11, - ] - : Overloads10; -type Overloads10 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; - (...args: infer A10): infer R10; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - (...args: A10) => R10, - ] - : Overloads9; -type Overloads9 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; - (...args: infer A9): infer R9; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - (...args: A9) => R9, - ] - : Overloads8; -type Overloads8 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; - (...args: infer A8): infer R8; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - (...args: A8) => R8, - ] - : Overloads7; -type Overloads7 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; - (...args: infer A7): infer R7; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - (...args: A7) => R7, - ] - : Overloads6; -type Overloads6 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; - (...args: infer A6): infer R6; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - (...args: A6) => R6, - ] - : Overloads5; -type Overloads5 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; - (...args: infer A5): infer R5; -} - ? [ - (...args: A1) => R1, - (...args: A2) => R2, - (...args: A3) => R3, - (...args: A4) => R4, - (...args: A5) => R5, - ] - : Overloads4; -type Overloads4 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; - (...args: infer A4): infer R4; -} - ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4] - : Overloads3; -type Overloads3 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; - (...args: infer A3): infer R3; -} - ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3] - : Overloads2; -type Overloads2 = T extends { - (...args: infer A1): infer R1; - (...args: infer A2): infer R2; -} - ? [(...args: A1) => R1, (...args: A2) => R2] - : Overloads1; -type Overloads1 = T extends { - (...args: infer A1): infer R1; -} - ? [(...args: A1) => R1] - : unknown; From a48b97024f3ea2443c0f21721b5316cded2de9de Mon Sep 17 00:00:00 2001 From: Peter Sanderson Date: Tue, 3 Dec 2024 16:00:50 +0100 Subject: [PATCH 2/4] chore: split, improve and clarify types in type-utils --- packages/type-utils/src/array.ts | 10 +++ packages/type-utils/src/index.ts | 6 +- packages/type-utils/src/object.ts | 13 +++ packages/type-utils/src/overloads.ts | 25 ++++++ packages/type-utils/src/utils.ts | 116 +++++++++++++++++---------- 5 files changed, 126 insertions(+), 44 deletions(-) create mode 100644 packages/type-utils/src/array.ts create mode 100644 packages/type-utils/src/object.ts diff --git a/packages/type-utils/src/array.ts b/packages/type-utils/src/array.ts new file mode 100644 index 00000000000..8c37cb8f070 --- /dev/null +++ b/packages/type-utils/src/array.ts @@ -0,0 +1,10 @@ +/** + * Gets the type of the elements in an array. + * + * Example: + * ``` + * type A = (number | string)[]; + * type E = ArrayElement; // number | string + * ``` + */ +export type ArrayElement = ArrayType[number]; diff --git a/packages/type-utils/src/index.ts b/packages/type-utils/src/index.ts index 983b52309aa..c029bb02a64 100644 --- a/packages/type-utils/src/index.ts +++ b/packages/type-utils/src/index.ts @@ -1,3 +1,5 @@ -export * from './utils'; -export * from './timeout'; +export * from './array'; export * from './overloads'; +export * from './timeout'; +export * from './utils'; +export * from './object'; diff --git a/packages/type-utils/src/object.ts b/packages/type-utils/src/object.ts new file mode 100644 index 00000000000..1f18d908681 --- /dev/null +++ b/packages/type-utils/src/object.ts @@ -0,0 +1,13 @@ +export type GetObjectWithKey = U extends object + ? K extends keyof U + ? U + : never + : never; + +export type GetObjectWithoutKey = U extends object + ? K extends keyof U + ? never + : U + : never; + +export type ObjectsOnly = T extends Record ? T : never; diff --git a/packages/type-utils/src/overloads.ts b/packages/type-utils/src/overloads.ts index ef5e952b917..f8f47579dc2 100644 --- a/packages/type-utils/src/overloads.ts +++ b/packages/type-utils/src/overloads.ts @@ -1,5 +1,7 @@ export type OverloadImplementations = Overloads; + export type Overloads = Overloads24; + type Overloads24 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -53,6 +55,7 @@ type Overloads24 = T extends { (...args: A24) => R24, ] : Overloads23; + type Overloads23 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -104,6 +107,7 @@ type Overloads23 = T extends { (...args: A23) => R23, ] : Overloads22; + type Overloads22 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -153,6 +157,7 @@ type Overloads22 = T extends { (...args: A22) => R22, ] : Overloads21; + type Overloads21 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -200,6 +205,7 @@ type Overloads21 = T extends { (...args: A21) => R21, ] : Overloads20; + type Overloads20 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -245,6 +251,7 @@ type Overloads20 = T extends { (...args: A20) => R20, ] : Overloads19; + type Overloads19 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -288,6 +295,7 @@ type Overloads19 = T extends { (...args: A19) => R19, ] : Overloads18; + type Overloads18 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -329,6 +337,7 @@ type Overloads18 = T extends { (...args: A18) => R18, ] : Overloads17; + type Overloads17 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -368,6 +377,7 @@ type Overloads17 = T extends { (...args: A17) => R17, ] : Overloads16; + type Overloads16 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -405,6 +415,7 @@ type Overloads16 = T extends { (...args: A16) => R16, ] : Overloads15; + type Overloads15 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -440,6 +451,7 @@ type Overloads15 = T extends { (...args: A15) => R15, ] : Overloads14; + type Overloads14 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -473,6 +485,7 @@ type Overloads14 = T extends { (...args: A14) => R14, ] : Overloads13; + type Overloads13 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -504,6 +517,7 @@ type Overloads13 = T extends { (...args: A13) => R13, ] : Overloads12; + type Overloads12 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -533,6 +547,7 @@ type Overloads12 = T extends { (...args: A12) => R12, ] : Overloads11; + type Overloads11 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -560,6 +575,7 @@ type Overloads11 = T extends { (...args: A11) => R11, ] : Overloads10; + type Overloads10 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -585,6 +601,7 @@ type Overloads10 = T extends { (...args: A10) => R10, ] : Overloads9; + type Overloads9 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -608,6 +625,7 @@ type Overloads9 = T extends { (...args: A9) => R9, ] : Overloads8; + type Overloads8 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -629,6 +647,7 @@ type Overloads8 = T extends { (...args: A8) => R8, ] : Overloads7; + type Overloads7 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -648,6 +667,7 @@ type Overloads7 = T extends { (...args: A7) => R7, ] : Overloads6; + type Overloads6 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -665,6 +685,7 @@ type Overloads6 = T extends { (...args: A6) => R6, ] : Overloads5; + type Overloads5 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -680,6 +701,7 @@ type Overloads5 = T extends { (...args: A5) => R5, ] : Overloads4; + type Overloads4 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -688,6 +710,7 @@ type Overloads4 = T extends { } ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4] : Overloads3; + type Overloads3 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; @@ -695,12 +718,14 @@ type Overloads3 = T extends { } ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3] : Overloads2; + type Overloads2 = T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; } ? [(...args: A1) => R1, (...args: A2) => R2] : Overloads1; + type Overloads1 = T extends { (...args: infer A1): infer R1; } diff --git a/packages/type-utils/src/utils.ts b/packages/type-utils/src/utils.ts index c04847af02c..6222560cef1 100644 --- a/packages/type-utils/src/utils.ts +++ b/packages/type-utils/src/utils.ts @@ -1,20 +1,55 @@ -// make key required - +/** + * Make property of the object required. + * + * Example: + * ``` + * type T = { a?: number }; + * const t: RequiredKey = { a: 0 }; // 'a' is mandatory + * ``` + */ export type RequiredKey = Omit & Required>; -// object values types +/** + * Get type of the object values. + * + * Example: + * ``` + * type T = { a: number; b: string }; + * type V: ObjectValues; // number | string + * ``` + */ export type ObjectValues = T[keyof T]; -// all keys of types in an union +/** + * All keys of types in a union. + * + * Example: + * ``` + * type T = { a: number; b: string }; + * type K: Keys; // 'a' | 'b' + * ``` + */ export type Keys = T extends any ? keyof T : never; -// remove the keys while keeping the union +/** + * Distributes the Omit across a union. using distributive conditional types to achieve this: + * @see: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types + * @source: https://stackoverflow.com/questions/57103834/typescript-omit-a-property-from-all-interfaces-in-a-union-but-keep-the-union-s#answer-57103940 + * + * Example: + * ``` + * type T = { remove: string; keep1: number } | { remove: string; keep2: boolean }; + * type W = Without; + * const w: W = { keep1: 1, keep2: true }; + * ``` + */ export type Without = T extends any ? Omit : never; -// array element type -export type ArrayElement = ArrayType[number]; - -// const with optional types +/** + * Const with optional types. + * Todo: add example and better explanation. + * It has exactly 1 usage so maybe this shall even be here in the utils package + */ export type ConstWithOptionalFields< Const extends { [key: string]: any }, Fields extends string | number | symbol, @@ -30,49 +65,46 @@ export type ConstWithOptionalFields< }; }; -// Extract item from union -export type ItemExtractor = Extract; - -// Unwrap type from Promise -export type Await = T extends { - then(onfulfilled?: (value: infer U) => unknown): unknown; -} - ? U - : T; - +/** + * Recursively makes all properties of the object optional. If the nested property is an object, + * it will make its properties optional as well. + * + * Example: + * ``` + * type T = { a: number; b: { c: string; d: number } }; + * type P = DeepPartial; + * const p: P = { b: { d: 1 } }; // As everything is deeply optional + * ``` + */ export type DeepPartial = T extends () => any ? T : T extends { [key: string]: any } ? { [P in keyof T]?: DeepPartial } : T; +/** + * Type containing all primitive types in TypeScript. + */ export type PrimitiveType = string | number | boolean | Date | null | undefined; -// Record with optional key and required value. -// example of using partial union as keys: -// const p: PartialRecord<'a' | 'b' | 'c', string>; = { b: 'value' }; -export type PartialRecord = { - [P in K]?: T; -}; - -// distributive conditional types to the rescue! This way we can infer union literal type from ReturnType but exclude undefined +/** + * Record with optional key and required value. + * + * Example: + * ``` + * const p: PartialRecord<'a' | 'b' | 'c', string>; = { b: 'value' }; + * ``` + */ +export type PartialRecord = { [P in K]?: T }; + +/** + * This infers the union literal type from ReturnType but exclude undefined + */ export type DefinedUnionMember = T extends string ? T : never; -export type GetObjectWithKey = U extends object - ? K extends keyof U - ? U - : never - : never; -export type GetObjectWithoutKey = U extends object - ? K extends keyof U - ? never - : U - : never; - -export type ObjectsOnly = T extends Record ? T : never; - -// map object `T` to a narrowed type with only those entries that match given `ValueFilter` type +/** + * Map object `T` to a narrowed type with only those entries that match given `ValueFilter` type + */ export type FilterPropertiesByType = { [Key in keyof T as T[Key] extends ValueFilter ? Key : never]: T[Key]; }; - From 684cdb4aeb5161e028032f6ed99f5f9b90846f02 Mon Sep 17 00:00:00 2001 From: Peter Sanderson Date: Tue, 3 Dec 2024 16:04:51 +0100 Subject: [PATCH 3/4] chore: use type from package and remove the duplicated type --- packages/blockchain-link/src/workers/electrum/index.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/blockchain-link/src/workers/electrum/index.ts b/packages/blockchain-link/src/workers/electrum/index.ts index bc732d36a94..e28d3ae84e8 100644 --- a/packages/blockchain-link/src/workers/electrum/index.ts +++ b/packages/blockchain-link/src/workers/electrum/index.ts @@ -2,6 +2,7 @@ import { CustomError } from '@trezor/blockchain-link-types/src/constants/errors' import { MESSAGES, RESPONSES } from '@trezor/blockchain-link-types/src/constants'; import type { Response } from '@trezor/blockchain-link-types'; import { Message } from '@trezor/blockchain-link-types/src/messages'; +import { Without } from '@trezor/type-utils'; import { BaseWorker, CONTEXT, ContextType } from '../baseWorker'; import * as M from './methods'; @@ -13,9 +14,6 @@ import type { ElectrumClient } from './client/electrum'; type BlockListener = ReturnType; type TxListener = ReturnType; -// reason: -// https://stackoverflow.com/questions/57103834/typescript-omit-a-property-from-all-interfaces-in-a-union-but-keep-the-union-s#answer-57103940 -type Without = T extends any ? Omit : never; type Request = T extends any ? T & ContextType & { blockListener: BlockListener; txListener: TxListener } : never; From 5e6545e7402b63286c745b699cba307b36690846 Mon Sep 17 00:00:00 2001 From: Peter Sanderson Date: Tue, 3 Dec 2024 16:08:06 +0100 Subject: [PATCH 4/4] chore: use Keys type and remove duplicated type def --- packages/utils/src/mergeDeepObject.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/mergeDeepObject.ts b/packages/utils/src/mergeDeepObject.ts index 58e5bec4a24..879b7048d6a 100644 --- a/packages/utils/src/mergeDeepObject.ts +++ b/packages/utils/src/mergeDeepObject.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-use-before-define */ // code shamelessly stolen from https://github.com/voodoocreation/ts-deepmerge -type TAllKeys = T extends any ? keyof T : never; +import { Keys } from '@trezor/type-utils'; type TIndexValue = T extends any ? K extends keyof T @@ -22,7 +22,7 @@ type TMerged = [T] extends [Array] : [T] extends [TPrimitives] ? T : [T] extends [object] - ? TPartialKeys<{ [K in TAllKeys]: TMerged> }, never> + ? TPartialKeys<{ [K in Keys]: TMerged> }, never> : T; // istanbul ignore next