diff --git a/.gitignore b/.gitignore index f63a763..aeea740 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,7 @@ *~ # Typescript # emitted code -build/* -!build/package.json +build # ---> Node # Logs diff --git a/build/package.json b/build/package.json deleted file mode 100644 index 3dbc1ca..0000000 --- a/build/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "type": "module" -} diff --git a/package.json5 b/package.json5 index 68400a2..959a089 100644 --- a/package.json5 +++ b/package.json5 @@ -20,12 +20,12 @@ }, dependencies: { 'reflect-metadata': '0.1.13', + 'typescript-rtti': '0.8.3', }, devDependencies: { '@types/node': '18.11.18', 'ts-node': '10.9.1', ttypescript: '1.5.15', typescript: '4.7.4', - 'typescript-rtti': '0.8.3', }, } diff --git a/src/index.ts b/src/index.ts index 5d8c0e7..13c39fa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,11 @@ +import 'reflect-metadata' + import {Dispatcher} from './core/Dispatcher.js' import * as Specifications from './all.js' import {Complex} from './Complex/type.js' import {absquare as absquare_complex} from './Complex/arithmetic.js' -import 'reflect-metadata' -import { ReflectedFunctionParameter, ReflectedObjectRef, reflect } from 'typescript-rtti' +import { CallSite, ReflectedObjectRef, reflect } from 'typescript-rtti' import { square } from './generic/arithmetic.js' // verify that typescript-rtti works (just as experiment) @@ -21,29 +22,55 @@ console.log() // returnType class Number // try out a very simple case (just as experiment) -function createSquare(deps: { - multiply: (a: number, b: number) => number, - subtract: (a: number, b: number) => number +function createSquare(deps: { + multiply: (a: number, b: number) => number, + subtract: (a: number, b: number) => number }) { return (a: number) => deps.multiply(a, a) } console.log('reflect createSquare') console.log('parameter names', reflect(createSquare).parameters.map(parameter => parameter.name)) -console.log('parameter[0]', (reflect(createSquare).parameters[0] as ReflectedFunctionParameter)) +// console.log('parameter[0]', (reflect(createSquare).parameters[0] as ReflectedFunctionParameter)) // @ts-ignore console.log('parameterTypes[0]', (reflect(createSquare).parameterTypes[0] as ReflectedObjectRef)._ref.m) -console.log('parameterTypes[0][0]', +console.log('parameterTypes[0].ref.m[0]', // @ts-ignore (reflect(createSquare).parameterTypes[0] as ReflectedObjectRef)._ref.m[0].n, // @ts-ignore (reflect(createSquare).parameterTypes[0] as ReflectedObjectRef)._ref.m[0] ) +console.log('parameterTypes[0].ref.m[0].t.m', + // @ts-ignore + (reflect(createSquare).parameterTypes[0] as ReflectedObjectRef)._ref.m[0].t.m +) // @ts-ignore -console.log('parameters[0]', reflect(createSquare).parameters[0]) +// console.log('parameters[0]', reflect(createSquare).parameters[0]) // FIXME: where to find the information of the types of the dependencies multiply and subtract? +// Test whether we loose the type information when casting to a generic interface +// Conclusion: we keep the information, that is good. +console.log() +console.log('reflect createFunction') +type MathjsDependencies = Record +type MathjsCreateFunction = (deps: MathjsDependencies) => Function +const createFunction: MathjsCreateFunction = createSquare as MathjsCreateFunction +console.log('parameter names', reflect(createFunction).parameters.map(parameter => parameter.name)) +// @ts-ignore +console.log('parameterTypes[0]', (reflect(createFunction).parameterTypes[0] as ReflectedObjectRef)._ref.m) + +console.log() +console.log('CallSite') +function foo(call? : CallSite) { + console.log(reflect(call).typeParameters[0].isClass(String)) +} + +function bar(call? : CallSite) { + foo(); +} +bar(); + // FIXME: get all types out of Specifications // console.log('Specifications', reflect(Specifications)) // Throws errors diff --git a/tsconfig.json b/tsconfig.json index 4470b45..c761525 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "allowJs": false, "noImplicitAny": false, "moduleResolution": "Node", + "module": "commonjs", "plugins": [ { "transform": "typescript-rtti/dist/transformer"