From 58f0250ab40607f995e7abf323a98caa4afa90cd Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Wed, 16 Aug 2023 12:57:48 -0700 Subject: [PATCH] issue: deepkit punts on complicated template instantions/intersections The most troubling example is found in `src/complex/arithmetic.ts`, where (as one can see by carefully screening the output when you run src/index.ts) deepkit reports the type of ``` Dependencies<'absquare', X> & Dependencies<'add', Returns<'absquare', X>> ``` to be `any` (look for the line starting "Because { kind: 1," in the output; kind: 1 is deepkit's code for `any`. It doesn't even give a syntactic description of the type as say the intersection of two instances of the Dependencies generic type, which would be good enough (if we got the string parameters to the generics). --- src/Complex/arithmetic.ts | 8 ++++++++ src/core/Dispatcher.ts | 2 +- src/index.ts | 21 +++++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Complex/arithmetic.ts b/src/Complex/arithmetic.ts index 90c8eab..1426b45 100644 --- a/src/Complex/arithmetic.ts +++ b/src/Complex/arithmetic.ts @@ -1,3 +1,4 @@ +import {ReflectionFunction, stringifyType, typeOf} from '@deepkit/type' import {Complex} from './type.js' import type { Dependencies, Signature, Returns, RealType, AliasOf @@ -53,6 +54,13 @@ export const absquare = Signature<'absquare', Complex> => z => dep.add(dep.absquare(z.re), dep.absquare(z.im)) +const asRefl = ReflectionFunction.from(absquare) +console.log(' Here I only know that:', asRefl, asRefl.getParameterNames(), asRefl.getParameterType('dep'), stringifyType(asRefl.getParameterType('dep'))) + +class X {} + +console.log(' Because', typeOf & Dependencies<'add', Returns<'absquare', X>>>()) + export const divideReal = (dep: Dependencies<'divideReal' | 'complex', T>): Signature<'divideReal', Complex> => diff --git a/src/core/Dispatcher.ts b/src/core/Dispatcher.ts index def1bd3..d5a1955 100644 --- a/src/core/Dispatcher.ts +++ b/src/core/Dispatcher.ts @@ -33,7 +33,7 @@ type TypeSpecification = { } type SpecObject = Record -type SpecificationsGroup = Record +export type SpecificationsGroup = Record export class Dispatcher { installSpecification( diff --git a/src/index.ts b/src/index.ts index bc07f42..09d8855 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,27 @@ import { - typeOf, ReflectionFunction, ReflectionClass, ReflectionKind, stringifyType + typeOf, ReflectionFunction, ReflectionClass, ReflectionKind, stringifyType, + ReceiveType, resolveReceiveType } from '@deepkit/type' -import {Dispatcher} from './core/Dispatcher.js' +import {SpecificationsGroup, Dispatcher} from './core/Dispatcher.js' import * as Specifications from './all.js' +const asRefl = ReflectionFunction.from(Specifications.complex.absquare) +console.log(' all I know is:', asRefl, asRefl.getParameterNames(), asRefl.getParameterType('dep')) + +function analyze( + spec: SpecificationsGroup, type?: ReceiveType +) { + type = resolveReceiveType(type) + console.log(stringifyType(type)) + console.log('is the type of') + console.log(spec) + //@ts-ignore + console.log('In particular', spec.complex.absquare.__type) +} + +analyze(Specifications) + export default new Dispatcher(Specifications) import {Complex} from './Complex/type.js'