javascript - Kotlin JavaScript 到 TypeScript 定义文件

我找到了ts2kt library这将从任意 .d.ts 文件创建 Kotlin 头文件。但是,我想往相反的方向走。

我想构建一个可以编译为 JavaScript 的 Kotlin 库,但我想从 TypeScript 中使用它。有没有办法让 Kotlin 生成一个 .d.ts 来自其公开接口(interface)的文件?我是否以正确的方式处理这个问题?

最佳答案

In Kotlin 1.4-M1 support for exporting TypeScript definitions was added

Preview: TypeScript definitions

Another feature in the new Kotlin/JS IR compiler we’re excited to show off is the generation of TypeScript definitions from Kotlin code. These definitions can be used by JavaScript tools and IDEs when working on hybrid apps to provide autocompletion, support static analyzers, and make it easier to include Kotlin code in JS and TS projects.

For top-level declarations marked with @JsExport (see above) in a project configured to use produceExecutable(), a .d.ts file with the TypeScript definitions will be generated. For the snippet above, they look like this:

// [...]
namespace blogpost {
    class KotlinGreeter {
        constructor(who: string)
        greet(): string
    }
    function farewell(who: string): string
}
// [...]

In Kotlin 1.4-M1, these declarations can be found in build/js/packages/<package_name>/kotlin alongside the corresponding, un-webpacked JavaScript code. Please note that since this is only a preview, they are not added to the distributions folder by default for now. You can expect this behavior to change in the future.

https://stackoverflow.com/questions/42537677/

相关文章:

arrays - 如何比较 Kotlin 中的两个数组?

android - IllegalArgumentException : Parameter spe

android - 无法使用 Android Studio 3.0 + DataBinding +

android - Kotlin Coroutines 在 Android 中的正确方式

kotlin - 如何在 Kotlin 中向 Java 类添加静态方法

gradle - 如何将 Gradle 项目中 Kotlin 的字节码版本设置为 Java 8?

arrays - 字符串数组文字?我如何简单地编码?

java - Android Studio 3.0 金丝雀 1 : Kotlin tests or

kotlin - Dagger 和 Kotlin 。 Dagger 不生成组件类

kotlin - Kotlin 中的扩展字段