typocomath/src/index.ts

67 lines
2.5 KiB
TypeScript
Raw Normal View History

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'
2023-01-25 13:42:23 +00:00
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
const quatAbsquare = absquare_complex({
add: mockRealAdd,
absquare: mockComplexAbsquare
})
const myabs = quatAbsquare({re: {re: 0, im: 1}, im: {re:2, im: 3}})
const typeTest: typeof myabs = 7 // check myabs is just a number
console.log('Result is', myabs)