Set up typescript-rtti (WIP)
This commit is contained in:
parent
cc1e66c054
commit
35a8c62ff2
9 changed files with 273 additions and 14 deletions
|
@ -1,3 +1,6 @@
|
|||
import "reflect-metadata"
|
||||
import { reflect, type CallSite } from 'typescript-rtti'
|
||||
|
||||
/* A Dispatcher is a collection of operations that do run-time
|
||||
* dispatch on the types of their arguments. Thus, every individual
|
||||
* method is like a typed-function (from the library by that name),
|
||||
|
@ -46,6 +49,10 @@ export class Dispatcher {
|
|||
// that's really possible, though.
|
||||
) {
|
||||
console.log('Pretending to install', name, signature, '=>', returns)
|
||||
|
||||
// @ts-ignore
|
||||
console.log(name, 'signature', reflect(signature))
|
||||
console.log(name, 'dependencies', reflect(dependencies))
|
||||
//TODO: implement me
|
||||
}
|
||||
installType(name: TypeName, typespec: TypeSpecification) {
|
||||
|
|
52
src/index.ts
52
src/index.ts
|
@ -1,11 +1,57 @@
|
|||
import {Dispatcher} from './core/Dispatcher.js'
|
||||
import * as Specifications from './all.js'
|
||||
|
||||
export default new Dispatcher(Specifications)
|
||||
|
||||
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 { square } from './generic/arithmetic.js'
|
||||
|
||||
// verify that typescript-rtti works (just as experiment)
|
||||
const add = (a: number, b: number): number => a + b
|
||||
console.log('reflect add')
|
||||
console.log('parameterNames', reflect(add).parameterNames)
|
||||
console.log('parameterTypes', reflect(add).parameterTypes.map(type => type.toString()))
|
||||
console.log('returnType', reflect(add).returnType.toString())
|
||||
console.log()
|
||||
// output:
|
||||
// reflect function add
|
||||
// parameterNames [ 'a', 'b' ]
|
||||
// parameterTypes [ 'class Number', 'class Number' ]
|
||||
// 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
|
||||
}) {
|
||||
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))
|
||||
// @ts-ignore
|
||||
console.log('parameterTypes[0]', (reflect(createSquare).parameterTypes[0] as ReflectedObjectRef)._ref.m)
|
||||
console.log('parameterTypes[0][0]',
|
||||
// @ts-ignore
|
||||
(reflect(createSquare).parameterTypes[0] as ReflectedObjectRef)._ref.m[0].n,
|
||||
// @ts-ignore
|
||||
(reflect(createSquare).parameterTypes[0] as ReflectedObjectRef)._ref.m[0]
|
||||
)
|
||||
// @ts-ignore
|
||||
console.log('parameters[0]', reflect(createSquare).parameters[0])
|
||||
// FIXME: where to find the information of the types of the dependencies multiply and subtract?
|
||||
|
||||
|
||||
|
||||
// FIXME: get all types out of Specifications
|
||||
// console.log('Specifications', reflect(Specifications)) // Throws errors
|
||||
|
||||
|
||||
// TODO: import all specificiations (turned off for debugging purposes)
|
||||
// export default new Dispatcher(Specifications)
|
||||
|
||||
|
||||
const mockRealAdd = (a: number, b: number) => a+b
|
||||
const mockComplexAbsquare = (z: Complex<number>) => z.re*z.re + z.im*z.im
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ export const absquare: Signature<'absquare', number> = a => a * a
|
|||
export const reciprocal: Signature<'reciprocal', number> = a => 1 / a
|
||||
export const divide: Signature<'divide', number> = (a, b) => a / b
|
||||
|
||||
const basicSqrt = a => isNaN(a) ? NaN : Math.sqrt(a)
|
||||
const basicSqrt = (a: number) => isNaN(a) ? NaN : Math.sqrt(a)
|
||||
export const conservativeSqrt: Signature<'conservativeSqrt', number> = basicSqrt
|
||||
|
||||
export const sqrt =
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue