ScalablyTyped / Converter

Typescript to Scala.js converter
https://scalablytyped.org
GNU General Public License v3.0
220 stars 42 forks source link

In `@types/node`, fs.WriteStream does not extend stream.Writable #564

Open unarist opened 1 year ago

unarist commented 1 year ago

In below code, stream.end() should be compiled as fs.WriteStream extends stream.Writable which has end() method. However, this won't be compiled without asInstanceOf[].

object Main {
  def main(args: Array[String]): Unit = {
    val stream = typings.node.fsMod.createWriteStream("output.txt")
    // stream.end()  // value end is not a member of typings.node.fsMod.WriteStream
    stream.asInstanceOf[typings.node.streamMod.Writable].end()
  }
}

ST generated source:

@JSImport("fs", "WriteStream")
@js.native
class WriteStream () extends StObject {
  def this(opts: WritableOptions) = this()
...

The corresponding d.ts should be this. I'm not sure why ScalablyTyped cannot resolve this extends, but maybe import * as stream from 'node:stream' is the case.

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/61694a446616874377bc7b87b8644d8eed2da19a/types/node/v18/fs.d.ts#L450 https://github.com/DefinitelyTyped/DefinitelyTyped/blob/61694a446616874377bc7b87b8644d8eed2da19a/types/node/v18/fs.d.ts#L22 https://github.com/DefinitelyTyped/DefinitelyTyped/blob/61694a446616874377bc7b87b8644d8eed2da19a/types/node/v18/stream.d.ts#L1455-L1457

build configuration

plugins.sbt

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.2")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.21.1")
addSbtPlugin("org.scalablytyped.converter" % "sbt-converter" % "1.0.0-beta42")

build.sbt

scalaVersion := "2.13.11"

enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin, ScalablyTypedConverterPlugin)
scalaJSUseMainModuleInitializer := true

Compile / npmDependencies += "@types/node" -> "18.16.19"

stTypescriptVersion := "5.0.4"
stStdlib := List("es2022")
stUseScalaJsDom := false
oyvindberg commented 1 year ago

Thanks for the report!

I'm aware there has been come imperfections with those classes. One case I remember in particular is that deep in that complicated hierarchy there is a Duplex class which extends what ST needs to consider two classes. This is not permitted, so one of them is dropped.

This may be something different though, I'll look into eventually. For now the cast is a good workaround