chore: use direct function definitions instead of { params, returns }

This commit is contained in:
Jos de Jong 2023-01-04 12:28:24 +01:00
parent a0b21181e6
commit 1b9d6b6428
6 changed files with 27 additions and 32 deletions

View File

@ -9,8 +9,8 @@ declare module "../interfaces/type" {
// after removing any `_...` suffixes; the following should be // after removing any `_...` suffixes; the following should be
// additional dispatches for add and divide, not separate // additional dispatches for add and divide, not separate
// operations, in the final mathjs bundle. // operations, in the final mathjs bundle.
add_real: {params: [T, RealType<T>], returns: T} add_real: (a: T, b: RealType<T>) => T
divide_real: {params: [T, RealType<T>], returns: T} divide_real: (a: T, b: RealType<T>) => T
} }
} }

View File

@ -32,7 +32,7 @@ declare module "../interfaces/type" {
} }
interface Signatures<T> { interface Signatures<T> {
complex: {params: [T] | [T,T], returns: Complex<T>} complex: ((re: T) => Complex<T>) | ((re: T, im: T) => Complex<T>)
} }
} }

View File

@ -1,23 +1,18 @@
import type {Complex} from '../Complex/type.js' import type {Complex} from '../Complex/type.js'
import type {RealType} from './type.js' import type {RealType} from './type.js'
type UnaryOperator<T> = {params: [T], returns: T}
type BinaryOperator<T> = {params: [T, T], returns: T}
declare module "./type" { declare module "./type" {
interface Signatures<T> { interface Signatures<T> {
add: BinaryOperator<T> add: (a: T, b: T) => T
unaryMinus: UnaryOperator<T> unaryMinus: (a: T) => T
conj: UnaryOperator<T> conj: (a: T) => T
subtract: BinaryOperator<T> subtract: (a: T, b: T) => T
multiply: BinaryOperator<T> multiply: (a: T, b: T) => T
square: UnaryOperator<T> square: (a: T) => T
absquare: {params: [T], returns: RealType<T>} absquare: (a: T) => RealType<T>
reciprocal: UnaryOperator<T> reciprocal: (a: T) => T
divide: BinaryOperator<T> divide: (a: T, b: T) => T
conservativeSqrt: UnaryOperator<T> conservativeSqrt: (a: T) => T
sqrt: { sqrt: (a: T)=> T extends Complex<unknown> ? T : T | Complex<T>
params: [T],
returns: T extends Complex<any> ? T : T | Complex<T>
}
} }
} }

View File

@ -1,9 +1,10 @@
// Warning: a module must have something besides just a "declare module" // Warning: a module must have something besides just a "declare module"
// section; otherwise it is ignored. // section; otherwise it is ignored.
export type UnaryPredicate<T> = {params: [T], returns: boolean} export type UnaryPredicate<T> = (a: T) => boolean
declare module "./type" { declare module "./type" {
interface Signatures<T> { interface Signatures<T> {
isReal: UnaryPredicate<T> isReal: (a: T) => boolean
isSquare: UnaryPredicate<T> isSquare: (a: T) => boolean
} }
} }

View File

@ -1,9 +1,9 @@
// Warning: a module must have something besides just a "declare module" // Warning: a module must have something besides just a "declare module"
// section; otherwise it is ignored. // section; otherwise it is ignored.
export type BinaryPredicate<T> = {params: [T, T], returns: boolean} export type BinaryPredicate<T> = (a: T, b: T) => T
declare module "./type" { declare module "./type" {
interface Signatures<T> { interface Signatures<T> {
equal: BinaryPredicate<T> equal: (a: T, b: T) => boolean
unequal: BinaryPredicate<T> unequal: (a: T, b: T) => boolean
} }
} }

View File

@ -58,17 +58,16 @@ export type RealType<T> = ALookup<T, 'real'>
* key 're' in the interface. * key 're' in the interface.
****/ ****/
export interface Signatures<T> { export interface Signatures<T> {
zero: {params: [T], returns: ZeroType<T>} zero: (a: T) => ZeroType<T>
one: {params: [T], returns: OneType<T>} one: (a: T) => OneType<T>
// nan needs to be able to operate on its own output for everything // nan needs to be able to operate on its own output for everything
// else to compile. That's why its parameter type is widened: // else to compile. That's why its parameter type is widened:
nan: {params: [T | NaNType<T>], returns: NaNType<T>} nan: (a: T | NaNType<T>) => NaNType<T>
re: {params: [T], returns: RealType<T>} re: (a: T) => RealType<T>
} }
type SignatureKey = keyof Signatures<unknown> type SignatureKey = keyof Signatures<unknown>
export type Returns<Name extends SignatureKey, T> = Signatures<T>[Name]['returns'] export type Signature<Name extends SignatureKey, T> = Signatures<T>[Name]
export type Signature<Name extends SignatureKey, T> = export type Returns<Name extends SignatureKey, T> = ReturnType<Signatures<T>[Name]>
(...args: Signatures<T>[Name]['params']) => Returns<Name, T>
export type Dependencies<Name extends SignatureKey, T> = {[K in Name]: Signature<K, T>} export type Dependencies<Name extends SignatureKey, T> = {[K in Name]: Signature<K, T>}