typocomath/src/generic/arithmetic.ts

28 lines
992 B
TypeScript

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') // 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>();
type Expand<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;
type Out = Expand<Dependencies<'multiply', number>>;
console.log("Deps type is", $$typeToString!<Out>())