From a8628c247394777a48faa4e6f63502bb184d8fb1 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Sat, 3 Feb 2024 10:01:47 +0000 Subject: [PATCH] Make getVariables tail recursive More correct, as per tail recursion in other places, but `variables.ts` didn't get much attention before --- src/variables.ts | 49 ++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/variables.ts b/src/variables.ts index da06d7b6..86e0c82a 100644 --- a/src/variables.ts +++ b/src/variables.ts @@ -67,27 +67,32 @@ type unwrapTypeRef = Type ext ? _unwrapTypeRefRec : null | _unwrapTypeRefRec; -type getVariablesRec = Variables extends [ - infer Variable, - ...infer Rest, -] - ? (Variable extends { kind: Kind.VARIABLE_DEFINITION; variable: any; type: any } - ? Variable extends { defaultValue: undefined; type: { kind: Kind.NON_NULL_TYPE } } - ? { - [Name in Variable['variable']['name']['value']]: unwrapTypeRef< - Variable['type'], - Introspection - >; - } - : { - [Name in Variable['variable']['name']['value']]?: unwrapTypeRef< - Variable['type'], - Introspection - >; - } - : {}) & - getVariablesRec - : {}; +type _getVariablesRec< + Variables, + Introspection extends IntrospectionLikeType, + VariablesObject = {}, +> = Variables extends [infer Variable, ...infer Rest] + ? _getVariablesRec< + Rest, + Introspection, + (Variable extends { kind: Kind.VARIABLE_DEFINITION; variable: any; type: any } + ? Variable extends { defaultValue: undefined; type: { kind: Kind.NON_NULL_TYPE } } + ? { + [Name in Variable['variable']['name']['value']]: unwrapTypeRef< + Variable['type'], + Introspection + >; + } + : { + [Name in Variable['variable']['name']['value']]?: unwrapTypeRef< + Variable['type'], + Introspection + >; + } + : {}) & + VariablesObject + > + : VariablesObject; type getVariablesType< Document extends DocumentNodeLike, @@ -96,7 +101,7 @@ type getVariablesType< kind: Kind.OPERATION_DEFINITION; variableDefinitions: any; } - ? obj> + ? obj<_getVariablesRec> : {}; export type { getVariablesType };