josdejong/mathjs

Type declarations do not understand typed-function's automatic conversions

Open

#2,582 opened on 2022年5月30日

GitHub で見る
 (8 comments) (0 reactions) (0 assignees)JavaScript (1,298 forks)batch import
bughelp wantedtypescript

Repository metrics

Stars
 (13,832 stars)
PR merge metrics
 (30d に merged PR はありません)

説明

For example, one signature of math.exp is Complex. But there is a conversion in mathjs from Fraction to Complex. Hence, math.exp(math.fraction(1,1)) is a legal expression that returns e + 0i. On the other hand, currently in typescript const complexe: Complex = math.exp(math.fraction(1, 1)) produces:

index.ts:142:40 - error TS2769: No overload matches this call.
  Overload 1 of 3, '(x: number): number', gave the following error.
    Argument of type 'Fraction' is not assignable to parameter of type 'number'.
  Overload 2 of 3, '(x: BigNumber): BigNumber', gave the following error.
    Argument of type 'Fraction' is not assignable to parameter of type 'BigNumber'.
      Type 'Fraction' is missing the following properties from type 'Decimal': e, toStringTag, absoluteValue, abs, and 97 more.
  Overload 3 of 3, '(x: Complex): Complex', gave the following error.
    Argument of type 'Fraction' is not assignable to parameter of type 'Complex'.
      Type 'Fraction' is missing the following properties from type 'Complex': re, im, clone, equals, and 6 more.

This example is of course just the tip of this iceberg (there are many other automatic conversions). It does not seem to me that a reasonable solution is to go through all of the functions in index.d.ts and add the legal signatures resulting from the conversions. But I am not sure what a reasonable approach would be. I imagine one could define a type ComplexOrConvertibleThereto (needs a better name) and change the parameters. But that can't be a simple search-and-replace, since for those functions that have a Fraction signature as well as Complex, the separate Fraction definition takes precedence over the one that would come from converting Fraction to Complex

コントリビューターガイド