Compare commits

..

6 Commits
main ... dktype

Author SHA1 Message Date
Glen Whitney 9abea5c484 issue: deepkit doesn't reflect types imported via `import type` 2023-08-16 21:46:10 -07:00
Glen Whitney 58f0250ab4 issue: deepkit punts on complicated template instantions/intersections
The most troubling example is found in `src/complex/arithmetic.ts`,
  where (as one can see by carefully screening the output when you
  run src/index.ts) deepkit reports the type of

  ```
  Dependencies<'absquare', X> & Dependencies<'add', Returns<'absquare', X>>
  ```

  to be `any` (look for the line starting "Because { kind: 1," in the output;
  kind: 1 is deepkit's code for `any`. It doesn't even give a syntactic
  description of the type as say the intersection of two instances of the
  Dependencies generic type, which would be good enough (if we got the
  string parameters to the generics).
2023-08-16 12:57:48 -07:00
Glen Whitney eff68e5ac2 doc: note possible need for extra installation step 2023-08-15 17:04:30 -07:00
Glen Whitney 479a484011 feat: Deepkit provides type info for case that typescript-rtti failed 2023-08-15 16:50:24 -07:00
Glen Whitney 16da30d865 doc: add command to create package.json in obj directory 2023-08-15 11:05:11 -07:00
Glen Whitney 7243277546 chore: update typescript to latest 2023-08-15 10:58:58 -07:00
24 changed files with 283 additions and 1279 deletions

2
.gitignore vendored
View File

@ -2,7 +2,7 @@
*~
# Typescript
# emitted code
build
obj
# ---> Node
# Logs

1
.npmrc
View File

@ -1 +0,0 @@
shell-emulator=true

View File

@ -2,13 +2,12 @@
A final (?) prototype for a refactor of mathjs, culminating the picomath, pocomath, typomath series. Provides an extensible core with "fuzzy" types for its operations, that can at any time generate exact .d.ts file for its current state.
To build and run the prototype, run:
Convenience scripts:
```
npx tsc
echo '{"type": "module"}' > obj/package.json
node obj
```
* `pnpm build` -- compile the package
* `pnpm exec` -- run the compiled code produced by `pnpm build`
* `pnpm go` -- both of the above in sequence.
Important installation note:
after `pnpm install`, you must execute `npx ts-patch install` to activate the ts-macros compiler plugin.
Installation note: after doing `pnpm install` if the above compilation/run commands don't seem to work, execute `npx deepkit-type-install` and try again.

View File

@ -1 +0,0 @@
{"type": "module"}

View File

@ -1,16 +1,11 @@
{
name: 'typocomath',
version: '0.0.2',
version: '0.0.1',
description: 'A hopeful final typescipt-pragmatic mathjs proof-of-concept',
main: 'index.ts',
scripts: {
test: 'echo "Error: no test specified" && exit 1',
build: 'mkdirp build && cpy etc/package.json build --flat && tsc',
start: 'node build',
go: 'pnpm clean && pnpm build && pnpm start',
clean: 'del-cli build',
},
packageManager: 'pnpm',
keywords: [
'math',
'algebra',
@ -23,13 +18,11 @@
url: 'https://code.studioinfinity.org/glen/typocomath.git',
},
devDependencies: {
'@types/node': '20.8.4',
'cpy-cli': '5.0.0',
'del-cli': '5.1.0',
mkdirp: '3.0.1',
'source-map': '0.7.4',
'ts-macros': '2.6.0',
'ts-patch': '3.0.2',
typescript: '5.1.6',
'@deepkit/type-compiler': '1.0.1-alpha.97',
'@types/node': '^20.5.0',
typescript: '^5.1.6',
},
dependencies: {
'@deepkit/type': '1.0.1-alpha.97',
},
}

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
import {ReflectionFunction, stringifyType, typeOf} from '@deepkit/type'
import {Complex} from './type.js'
import type {
Dependencies, Signature, Returns, RealType, AliasOf
} from '../interfaces/type.js'
import {$reflect} from '../interfaces/type.js'
declare module "../interfaces/type" {
interface Signatures<T> {
@ -54,6 +54,13 @@ export const absquare =
Signature<'absquare', Complex<T>> =>
z => dep.add(dep.absquare(z.re), dep.absquare(z.im))
const asRefl = ReflectionFunction.from(absquare)
console.log(' Here I only know that:', asRefl, asRefl.getParameterNames(), asRefl.getParameterType('dep'), stringifyType(asRefl.getParameterType('dep')))
class X {}
console.log(' Because', typeOf<Dependencies<'absquare', X> & Dependencies<'add', Returns<'absquare', X>>>())
export const divideReal =
<T>(dep: Dependencies<'divideReal' | 'complex', T>):
Signature<'divideReal', Complex<T>> =>
@ -79,10 +86,11 @@ export const sqrt =
& Dependencies<'zero' | 'complex', T>
& Dependencies<'absquare' | 're' | 'divideReal', Complex<T>>
& {
addTR: Signature<'addReal', T>,
addTR: Signature<'addReal', T>,
addRR: Signature<'add', RealType<T>>,
addCR: Signature<'addReal', Complex<T>>
}): Signature<'sqrt', Complex<T>> =>
}):
Signature<'sqrt', Complex<T>> =>
z => {
const myabs = dep.conservativeSqrt(dep.absquare(z))
const r = dep.re(z)
@ -98,7 +106,4 @@ export const sqrt =
return dep.divideReal(num, denom)
}
$reflect!([
add, addReal, unaryMinus, conj, subtract, multiply, absquare, divideReal,
reciprocal, divide, sqrt
])
export const conservativeSqrt = sqrt

View File

@ -1,13 +1,9 @@
import {Complex} from './type.js'
import type {Dependencies, Signature} from '../interfaces/type.js'
import {$reflect} from '../interfaces/type.js'
export const isReal =
<T>(dep: Dependencies<'add' | 'equal' | 'isReal', T>):
Signature<'isReal', Complex<T>> =>
z => dep.isReal(z.re) && dep.equal(z.re, dep.add(z.re, z.im))
export const isSquare = (): Signature<'isSquare', Complex<unknown>> =>
z => true // FIXME: not correct for Complex<bigint> once we get there
$reflect!([isReal, isSquare])
export const isSquare: Signature<'isSquare', Complex<any>> = z => true // FIXME: not correct for Complex<bigint> once we get there

View File

@ -1,8 +1,6 @@
import {Complex} from './type.js'
import {Dependencies, Signature} from '../interfaces/type.js'
import {$reflect} from '../interfaces/type.js'
export const equal =
<T>(dep: Dependencies<'equal', T>): Signature<'equal', Complex<T>> =>
(w, z) => dep.equal(w.re, z.re) && dep.equal(w.im, z.im)
$reflect!([equal])

View File

@ -2,12 +2,10 @@ import {joinTypes, typeOfDependency} from '../core/Dispatcher.js'
import type {
ZeroType, OneType, NaNType, Dependencies, Signature, Returns
} from '../interfaces/type.js'
import {$reflect} from '../interfaces/type.js'
export type Complex<T> = { re: T; im: T; }
export const Complex_type = {
name: 'Complex', // just until we have reflection to tell us
test: <T>(dep: { testT: (z: unknown) => z is T }) =>
(z: unknown): z is Complex<T> =>
typeof z === 'object' && z != null && 're' in z && 'im' in z
@ -63,5 +61,3 @@ export const nan =
export const re =
<T>(dep: Dependencies<'re', T>): Signature<'re', Complex<T>> =>
z => dep.re(z.re)
$reflect!([complex, zero, one, nan, re])

View File

@ -5,8 +5,6 @@
* for specific types (including their own).
*/
import {parseReflectedType, ImplementationDef} from './parseReflectedType.js'
// First helper types and functions for the Dispatcher
type TypeName = string
@ -27,7 +25,6 @@ export function joinTypes(a: TypeName, b: TypeName) {
// Now types used in the Dispatcher class itself
type TypeSpecification = {
name: string, // just until we get reflection, then we can remove this property
before?: TypeName[],
test: ((x: unknown) => boolean)
| ((d: DependenciesType) => (x: unknown) => boolean),
@ -36,49 +33,43 @@ type TypeSpecification = {
}
type SpecObject = Record<string, Function | TypeSpecification>
type SpecificationsGroup = Record<string, SpecObject>
export type SpecificationsGroup = Record<string, SpecObject>
export class Dispatcher {
installSpecification(
name: string,
defn: ImplementationDef,
signature: Signature,
returns: TypeName,
dependencies: Record<string, Signature>,
behavior: Function // possible todo: constrain this type based
// on the signature, return type, and dependencies. Not sure if
// that's really possible, though.
) {
console.log('Pretending to install', name, 'with signatures')
for (const signature of defn.fn.signatures) {
console.log(' ', signature.args, '=>', signature.returns)
}
if (defn.fn.aliasOf) {
console.log(' As an alias of', defn.fn.aliasOf)
}
console.log('Pretending to install', name, signature, '=>', returns)
//TODO: implement me
}
installType(name: TypeName, typespec: TypeSpecification) {
console.log('Pretending to install type', name, typespec)
//TODO: implement me
}
constructor(collection: SpecificationsGroup) {
const implementations = []
constructor(collection: SpecificationsGroup) {
for (const key in collection) {
console.log('Working on', key)
for (const identifier in collection[key]) {
const item = collection[key][identifier]
if (typeof item === 'function') {
implementations.push([key, identifier, item])
} else {
console.log('Handling type', key, ':', identifier)
console.log('Handling', key, ':', identifier)
const parts = identifier.split('_')
if (parts[parts.length - 1] === 'type') {
parts.pop()
const name = parts.join('_')
this.installType(
item.name, collection[key][identifier] as TypeSpecification)
name, collection[key][identifier] as TypeSpecification)
} else {
const name = parts[0]
this.installSpecification(
name, ['dunno'], 'unsure', {},
collection[key][identifier] as Function)
}
}
}
for (const trio of implementations) {
const [k, id, imp] = trio
console.log('Handling implementation', id, 'from', k)
this.installSpecification(
id, parseReflectedType(id, imp.reflectedType), imp)
}
}
}

View File

@ -1,279 +0,0 @@
export type FunctionDef = {
name: string,
aliasOf?: string,
signatures: Array<{
args: Array<{ name: string, type: string }>
returns: string
}>
}
export type ImplementationDef = {
fn: FunctionDef,
dependencies: Record<string, FunctionDef>
genericParameter: string | null
}
/**
* Parse a reflected type coming out of TypeScript into a structured object, for example:
*
* '<T>(dep: configDependency & { complex: ((re: number) => Complex<number>) | ((re: number, im: number) => Complex<number>); }) => (a: number) => number | Complex<number>'
*/
export function parseReflectedType(name: string, reflectedType: string): ImplementationDef {
console.log('For', name, 'parsing', reflectedType)
const [factoryArgs, fnsClause] = split(reflectedType, '=>', 2).map(trim)
const fn = parseAlias(name, fnsClause)
// extract the generic parameter like '<T>' at the start of the type
const genericParameter = factoryArgs.trim().startsWith('<')
? findBlockContents(factoryArgs, '<', '>')?.innerText || null
: null
const factoryArgsInner = findBlockContents(factoryArgs, '(', ')')
const depArg = split(factoryArgsInner.innerText, ':').map(trim)[1]
const depArgBlocks: string[] = depArg ? split(depArg, '&').map(trim) : []
const deps = depArgBlocks
.filter(depArgBlock => {
if (depArgBlock.startsWith('{') || depArgBlock === 'configDependency') {
return true
} else {
throw new SyntaxError(`Cannot parse dependency "${depArgBlock}"`)
}
})
.flatMap(parseDependencies)
const dependencies: Record<string, FunctionDef> = groupBy(deps, 'name')
return {fn, dependencies, genericParameter }
}
function parseDependencies(deps: string): FunctionDef[] {
if (deps === 'configDependency') {
return [{name: 'config', signatures: [{args: [], returns: 'Config'}]}]
}
const inner = findBlockContents(deps, '{', '}').innerText
return split(inner, ';')
.map(trim)
.filter(notEmpty)
.map(parseDependency)
}
// parse a dependency like "complex: ((re: number) => Complex<number>) | ((re: number, im: number) => Complex<number>)"
function parseDependency(dep: string): FunctionDef {
const [name, def] = split(dep, ':').map(trim)
return parseAlias(name, def)
}
// Parse a possibly aliased function
function parseAlias(name: string, alias: string): FunctionDef {
const { aliasOf, innerSignature } = parseAliasOf(alias)
return { name, signatures: parseSignatures(innerSignature), aliasOf }
}
// parse function signatures like ((re: number) => Complex<number>) | ((re: number, im: number) => Complex<number>)
// But also have to succeed on (a: number) => number | Complex<number>
// That's why we only split on an alternation bar `|` that's followed by
// a parenthesis; that way we avoid splitting a union return type. Note
// this is not necessarily foolproof, as there could be a return type that
// is a union with a complicated piece that has to be enclosed in parens;
// but so far it seems to work in practice.
function parseSignatures(sigs: string) {
return split(sigs, /[|]\s*(?=[(])/)
.map(trim)
.map(stripParenthesis)
.map(signature => {
const [argsBlock, returns] = split(signature, '=>').map(trim)
if (!returns) {
throw new SyntaxError(`Failed to find return type in '${signature}'`)
}
return {
args: parseArgs(argsBlock),
returns
}
})
}
// parse args like "(re: number, im: number)"
function parseArgs(argsBlock: string) : Array<{name: string, type: string}> {
const args = findBlockContents(argsBlock, '(', ')').innerText
return split(args, ',')
.map(trim)
.map(arg => {
const [name, type] = split(arg, ':').map(trim)
return { name, type}
})
}
// parse "AliasOf<"divide", (a: Complex<T>, b: RealType<Complex<T>>) => Complex<T>>"
function parseAliasOf(signature: string) : { innerSignature: string, aliasOf: string | undefined } {
if (!signature.startsWith('AliasOf')) {
return {
innerSignature: signature,
aliasOf: undefined
}
}
const inner = findBlockContents(signature, '<', '>').innerText.trim()
const [aliasOfWithQuotes, innerSignature] = split(inner, ',').map(trim)
return {
innerSignature,
aliasOf: aliasOfWithQuotes.substring(1, aliasOfWithQuotes.length - 1) // remove double quotes
}
}
// remove the outer parenthesis, for example "((re: number) => Complex<number>)" returns "(re: number) => Complex<number>"
function stripParenthesis(text: string) : string {
return text.startsWith('(') && text.endsWith(')')
? text.substring(1, text.length - 1)
: text
}
function findBlockContents(text: string, blockStart: string, blockEnd: string, startIndex = 0) : { start: number, end: number, innerText: string } | undefined {
let i = startIndex
while (!matchSubString(text, blockStart, i) && i < text.length) {
i++
}
if (i >= text.length) {
return undefined
}
i++
const start = i
while (!matchSubString(text, blockEnd, i) || matchSubString(text, '=>', i - 1)) {
i = skipBrackets(text, i)
i++
}
if (i >= text.length) {
return undefined
}
const end = i
return {
start,
end,
innerText: text.substring(start, end)
}
}
/**
* Given a string, generate a string source for a regexp that will match
* exactly the given string.
* Uses the fact that the only characters that need to be escaped in
* a character class are \, ], and ^
*/
function regexpQuote(s: string) {
const special = '\\]^'
let re = ''
for (const char of s) {
if (special.includes(char)) re += `\\${char}`
else re += `[${char}]`
}
return re
}
/**
* Split a string by a delimiter, but ignore all occurrences of the delimiter
* that are inside bracket pairs <> () [] {}
*/
export function split(
text: string, delimiter: string | RegExp, pieces = 0): string[] {
const delim: RegExp = typeof delimiter === 'string'
? new RegExp(regexpQuote(delimiter), 'y')
: new RegExp(delimiter.source, 'y')
const parts: string[] = []
let i = 0
let n = 1
let start = 0
while (i < text.length && (pieces === 0 || n < pieces)) {
i = skipBrackets(text, i)
delim.lastIndex = i
const result = delim.exec(text)
if (result) {
parts.push(text.substring(start, i))
n += 1
i += result[0].length
start = i
}
i++
}
parts.push(text.substring(start))
return parts
}
function skipBrackets(text: string, startIndex: number) : number {
let level = 0
let i = startIndex
do {
if (isBracketOpen(text, i)) {
level++
}
if (isBracketClose(text, i) && level > 0) {
level--
}
if (level === 0) {
break
}
i++
} while(i < text.length)
return i
}
function isBracketOpen(text: string, index: number) {
const char = text[index]
return char === '(' || char === '<' || char === '[' || char === '{'
}
function isBracketClose(text: string, index: number) {
const char = text[index]
// we need to take care of not matching the ">" of the operator "=>"
return char === ')' || (char === '>' && text[index - 1] !== '=') || char === ']' || char === '}'
}
function matchSubString(text: string, search: string, index: number) : boolean {
for (let i = 0; i < search.length; i++) {
if (text[i + index] !== search[i]) {
return false
}
}
return true
}
function trim(text: string) : string {
return text.trim()
}
function notEmpty(text: string) : boolean {
return text.length > 0
}
function groupBy<T>(items: T[], key: string) : Record<string, T> {
const obj: Record<string, T> = {}
items.forEach((item) => {
obj[item[key]] = item
})
return obj
}

View File

@ -1,9 +1,22 @@
import type {Dependencies, Signature} from '../interfaces/type.js'
import {$reflect} from '../interfaces/type.js'
import {ReflectionFunction, stringifyType, typeOf} from '@deepkit/type'
import type {Dependencies, Signature, Signatures, OneType} from '../interfaces/type.js'
interface LocalSignatures<T> {
one: (a: T) => OneType<T>
}
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
$reflect!([square])
const sqRefl = ReflectionFunction.from(square)
console.log(
' For generic square I know that:',
sqRefl,
sqRefl.getParameterNames(),
sqRefl.getParameterType('dep'),
"\n final dep type:",
stringifyType(sqRefl.getParameterType('dep')))
console.log(' because', typeOf<LocalSignatures<number>['one']>())
console.log(' readable:', stringifyType(typeOf<LocalSignatures<number>['one']>()))

View File

@ -1,7 +1,5 @@
import {Dependencies, Signature} from '../interfaces/type.js'
import {$reflect} from '../interfaces/type.js'
export const unequal =
<T>(dep: Dependencies<'equal', T>): Signature<'unequal', T> =>
(x, y) => !dep.equal(x, y)
$reflect!([unequal])

View File

@ -1,53 +1,73 @@
import {inspect} from 'node:util'
import {Dispatcher} from './core/Dispatcher.js'
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'
import {parseReflectedType} from './core/parseReflectedType.js'
const mockRealAdd = (a: number, b: number) => a+b
const mockComplexAbsquare = (z: Complex<number>) => z.re*z.re + z.im*z.im
const mockComplex = (re: number, im: number) => ({ re, im })
const config = {
predictable: false,
epsilon: 1e-14
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
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=', myabs)
const sqrt = Specifications.numbers.sqrt({
config,
complex: mockComplex
})
console.log('Result of sqrt(16)=', sqrt(16))
console.log('Result of sqrt(-4)=', sqrt(-4))
console.log()
console.log('1) NUMBER SQRT')
console.log(`1.1) REFLECTED TYPE: "${Specifications.numbers.sqrt.reflectedType}"`)
console.log(
'1.2) PARSED TYPE:',
inspect(
parseReflectedType('sqrt', Specifications.numbers.sqrt.reflectedType),
{ depth: null, colors: true }))
console.log()
console.log('2) GENERIC SQUARE')
console.log(`1.1) REFLECTED TYPE: "${Specifications.generic.square.reflectedType}"`)
console.log('2.2) PARSED TYPE:', inspect(parseReflectedType('square', Specifications.generic.square.reflectedType), { depth: null, colors: true }))
console.log()
console.log('3) COMPLEX SQRT')
console.log(`1.1) REFLECTED TYPE: "${Specifications.complex.sqrt.reflectedType}"`)
console.log('3.2) PARSED TYPE:', inspect(parseReflectedType('sqrt', Specifications.complex.sqrt.reflectedType), { depth: null, colors: true }))
console.log('Result is', myabs)

View File

@ -1,5 +1,3 @@
import {$$typeToString} from 'ts-macros'
/*****
* Every typocomath type has some associated types; they need
* to be published in the following interface. The key is the
@ -15,10 +13,6 @@ import {$$typeToString} from 'ts-macros'
* but that's OK, the generic parameter doesn't hurt in those cases.
****/
type ValueIntersectionByKeyUnion<T, TKey extends keyof T> = {
[P in TKey]: (k: T[P])=>void
} [TKey] extends ((k: infer I)=>void) ? I : never
export interface AssociatedTypes<T> {
undefined: {
type: undefined
@ -30,10 +24,10 @@ export interface AssociatedTypes<T> {
}
type AssociatedTypeNames = keyof AssociatedTypes<unknown>['undefined']
type ALookup<T, Name extends AssociatedTypeNames> = ValueIntersectionByKeyUnion<{
type ALookup<T, Name extends AssociatedTypeNames> = {
[K in keyof AssociatedTypes<T>]:
T extends AssociatedTypes<T>[K]['type'] ? AssociatedTypes<T>[K][Name] : unknown},
keyof AssociatedTypes<T>>
T extends AssociatedTypes<T>[K]['type'] ? AssociatedTypes<T>[K][Name] : never
}[keyof AssociatedTypes<T>]
// For everything to compile, zero and one must be subtypes of T:
export type ZeroType<T> = ALookup<T, 'zero'> & T
@ -75,16 +69,7 @@ export interface Signatures<T> {
type SignatureKey<T> = keyof Signatures<T>
export type Signature<Name extends SignatureKey<T>, T> = Signatures<T>[Name]
export type Returns<Name extends SignatureKey<T>, T> =
ReturnType<Signatures<T>[Name]>
type Deps<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;
export type Dependencies<Name extends SignatureKey<T>, T> =
Deps<{[K in Name]: Signature<K, T>}>
export type Returns<Name extends SignatureKey<T>, T> = ReturnType<Signatures<T>[Name]>
export type Dependencies<Name extends SignatureKey<T>, T> = {[K in Name]: Signature<K, T>}
export type AliasOf<Name extends string, T> = T & {aliasOf?: Name}
// For defining implementations with type reflection
export function $reflect<ImplTuple>(tup: ImplTuple) {
+[[tup], <T>(elt: T) =>
elt.reflectedType = $$typeToString!<T>(true, false, true)]
}

10
src/local.ts Normal file
View File

@ -0,0 +1,10 @@
import type {ImportedInterface} from './remote.js'
interface LocalInterface {
item: (a: number) => string
}
import {stringifyType, typeOf} from '@deepkit/type'
console.log('Locally:', stringifyType(typeOf<LocalInterface['item']>()))
console.log('Remotely:', stringifyType(typeOf<ImportedInterface['item']>()))

View File

@ -1,35 +1,25 @@
import type {configDependency} from '../core/Config.js'
import type {Dependencies, Signature} from '../interfaces/type.js'
import {$reflect} from '../interfaces/type.js'
export const add = (): Signature<'add', number> => (a, b) => a + b
const unaMinus = (a: number) => -a
export const unaryMinus = (): Signature<'unaryMinus', number> => unaMinus
export const conj = (): Signature<'conj', number> => a => a
export const subtract = (): Signature<'subtract', number> => (a, b) => a - b
export const multiply = (): Signature<'multiply', number> => (a, b) => a * b
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
export const add: Signature<'add', number> = (a, b) => a + b
export const unaryMinus: Signature<'unaryMinus', number> = a => -a
export const conj: Signature<'conj', number> = a => a
export const subtract: Signature<'subtract', number> = (a, b) => a - b
export const multiply: Signature<'multiply', number> = (a, b) => a * b
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: number) => isNaN(a) ? NaN : Math.sqrt(a)
export const conservativeSqrt = (): Signature<'conservativeSqrt', number> =>
basicSqrt
const basicSqrt = a => isNaN(a) ? NaN : Math.sqrt(a)
export const conservativeSqrt: Signature<'conservativeSqrt', number> = basicSqrt
export const sqrt =
(dep: configDependency
& Dependencies<'complex', number>): Signature<'sqrt', number> => {
if (dep.config.predictable || !dep.complex) {
return basicSqrt
}
return a => {
if (isNaN(a)) return NaN
if (a >= 0) return Math.sqrt(a)
return dep.complex(0, Math.sqrt(unaMinus(a)))
}
(dep: configDependency & Dependencies<'complex', number>):
Signature<'sqrt', number> => {
if (dep.config.predictable || !dep.complex) return basicSqrt
return a => {
if (isNaN(a)) return NaN
if (a >= 0) return Math.sqrt(a)
return dep.complex(0, Math.sqrt(unaryMinus(a)))
}
}
$reflect!([
add, unaryMinus, conj, subtract, multiply, absquare, reciprocal, divide,
conservativeSqrt, sqrt
])

View File

@ -1,4 +1,2 @@
export * from './type.js'
export * from './arithmetic.js'
export * from './predicate.js'
export * from './relational.js'

View File

@ -1,7 +1,4 @@
import type {Signature} from '../interfaces/type.js'
import {$reflect} from '../interfaces/type.js'
export const isReal = (): Signature<'isReal', number> => (a) => true
export const isSquare = (): Signature<'isSquare', number> => (a) => a >= 0
$reflect!([isReal, isSquare])
export const isReal: Signature<'isReal', number> = (a) => true
export const isSquare: Signature<'isSquare', number> = (a) => a >= 0

View File

@ -1,6 +1,5 @@
import {configDependency} from '../core/Config.js'
import {Signature} from '../interfaces/type.js'
import {$reflect} from '../interfaces/type.js'
const DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16
@ -20,4 +19,3 @@ export const equal =
return false
}
$reflect!([equal])

View File

@ -1,8 +1,6 @@
import type { Signature } from '../interfaces/type.js'
import {$reflect} from '../interfaces/type.js'
export const number_type = {
name: 'number', // just until we have reflection to tell us
before: ['Complex'],
test: (n: unknown): n is number => typeof n === 'number',
from: { string: (s: string) => +s }
@ -10,7 +8,7 @@ export const number_type = {
declare module "../interfaces/type" {
interface AssociatedTypes<T> {
number: {
numbers: {
type: number
zero: 0
one: 1
@ -21,9 +19,7 @@ declare module "../interfaces/type" {
}
// I don't like the redundancy of repeating 'zero'; any way to eliminate that?
export const zero = (): Signature<'zero', number> => (a) => 0
export const one = (): Signature<'one', number> => (a) => 1
export const nan = (): Signature<'nan', number> => (a) => NaN
export const re = (): Signature<'re', number> => (a) => a
$reflect!([zero, one, nan, re])
export const zero: Signature<'zero', number> = (a) => 0
export const one: Signature<'one', number> = (a) => 1
export const nan: Signature<'nan', number> = (a) => NaN
export const re: Signature<'re', number> = (a) => a

3
src/remote.ts Normal file
View File

@ -0,0 +1,3 @@
export interface ImportedInterface {
item: (a: number) => string
}

View File

@ -1,12 +1,9 @@
{
"compilerOptions": {
"target": "esnext",
"target": "ES2022",
"rootDir": "./src",
"outDir": "./build",
"moduleResolution": "nodenext",
"plugins": [ {
"transform": "ts-macros/dist/type-resolve",
"transformProgram": true
} ]
}
"outDir": "./obj",
"moduleResolution": "nodenext"
},
"reflection": true
}