scala-js-ts-importer icon indicating copy to clipboard operation
scala-js-ts-importer copied to clipboard

Support for object type literal

Open spaced opened this issue 10 years ago • 5 comments
trafficstars

In case of

[info] Running org.scalajs.tools.tsimporter.Main d3.d.ts d3.scala
Parse error at 1642.48
identifier expected
        export function ordinal<Domain extends { toString(): string }, Range>(): Ordinal<Domain, Range>;

spaced avatar Oct 06 '15 18:10 spaced

Oh my! That's not even an object type. It's a type refinement!

sjrd avatar Oct 06 '15 19:10 sjrd

Possible solution: a) do not support: exception b) ignore refinement c) ignore refinement if toString d) emit type refinement (does scala support it? generate trait?

spaced avatar Oct 07 '15 14:10 spaced

In case of d3.d.ts: Domain and Range are generic type arguments, they are placeholders for the types you need to provide rather than actual concrete types. Domain can be any type that has a toString. Have no idea how to handle it proper.

spaced avatar Oct 07 '15 15:10 spaced

@sjrd @spaced Would this not be the same?

  export function ordinal<Domain extends { toString(): string }, Range>():
 Ordinal<Domain, Range>;

->

def ordinal[Domain <: { def toString(): String }, Range]()
: Ordinal[Domain, Range]

I'm parsing it successfully in my fork (but ignoring it - treating it as js.Any), as rendering it to the above mentioned Scala seemed to be difficult...

KadekM avatar Oct 09 '16 05:10 KadekM

One might think so, but refinement types with arbitrary terms for js.Any are not really supported, because calling such members results in so-called reflective calls, which cannot be emitted correctly for JavaScript types.

sjrd avatar Oct 09 '16 06:10 sjrd