feat: Precisely reflect the type of an implementation at runtime (!!)

This commit is contained in:
Glen Whitney 2023-08-18 10:57:30 -07:00
parent 180e772dab
commit 770c302342
6 changed files with 192 additions and 2 deletions

View file

@ -1,7 +1,22 @@
import type {Dependencies, Signature} from '../interfaces/type.js'
import {$$typeToString} from 'ts-macros'
export const square =
<T>(dep: Dependencies<'multiply', T>): Signature<'square', T> =>
z => dep.multiply(z, z)
// z => dep.fooBar(z, z) // fails as desired
// z => dep.multiply(z, 'foo') // fails as desired
// z => dep.multiply(z, 'foo') // still fails as desired
// make sure ts-macros is running
function $contains<T>(value: T, ...possible: Array<T>) {
// repetition which goes over all the elements in the "possible" array
return +["||", [possible], (item: T) => value === item];
}
const searchItem = "needle";
console.log("In generic arithmetic")
console.log($contains!(searchItem, "erwin", "tj"));
// Transpiles to: console.log(false);
// OK, record the type of square:
square.reflectedType = $$typeToString!<typeof square>();

View file

@ -18,3 +18,6 @@ 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)
// Check type of the generic square implementation
console.log('Type of square is', Specifications.generic.square.reflectedType)