typocomath/src/index.ts

74 lines
2.4 KiB
TypeScript

import {
typeOf, ReflectionFunction, ReflectionClass, ReflectionKind, stringifyType,
ReceiveType, resolveReceiveType
} from '@deepkit/type'
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<SpecType>(
spec: SpecificationsGroup, type?: ReceiveType<SpecType>
) {
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<typeof Specifications>(Specifications)
export default new Dispatcher(Specifications)
import {Complex} from './Complex/type.js'
import {absquare as absquare_complex} from './Complex/arithmetic.js'
const mockRealAdd = (a: number, b: number) => a+b
const mockComplexAbsquare = (z: Complex<number>) => z.re*z.re + z.im*z.im
const addRefl = ReflectionFunction.from(mockRealAdd)
console.log('----> I figured out:', addRefl, addRefl.getParameterNames(), addRefl.getParameterType('b'))
function bungle(deps: {
factor: (i: number) => number,
label: string
}) {
return (a: number) => deps.label + (deps.factor(2)*a)
}
let fn = (i:number) => 3*i
console.log("\nBungling", bungle({factor: fn, label:'hextupled: '})(7))
const bRefl = ReflectionFunction.from(bungle)
const depstype = bRefl.getParameterType('deps')
console.log('Deps type is', depstype)
console.log(' aka ', stringifyType(depstype))
if (depstype.kind == ReflectionKind.objectLiteral) {
const depsRefl = new ReflectionClass(depstype)
console.log(' With methods', depsRefl.getMethodNames())
const factorRefl = depsRefl.getMethod('factor')
console.log(' And factor method takes parameters',
factorRefl.getParameterNames())
const itype = factorRefl.getParameterType('i')
console.log(' Parameter `i` has type', itype)
console.log(' aka', stringifyType(itype))
} else {
console.log(' Not understanding deps')
}
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)