简体   繁体   English

如何在 JavaScript 中使用/导入 TypeScript 声明文件?

[英]How to use/import TypeScript declaration files in JavaScript?

I'm playing a little bit with Kotlin Multiplatform.我正在玩一点 Kotlin Multiplatform。

Right now, I try to develop a Kotlin JS library to import and use it in an external JavaScript project.现在,我尝试开发一个 Kotlin JS 库来导入并在外部 JavaScript 项目中使用它。 This works fine so far.到目前为止,这工作正常。 Kotlin creates the JavaScript files from the Kotlin code, I can import and use them in the JavaScript project, and it does what it should. Kotlin creates the JavaScript files from the Kotlin code, I can import and use them in the JavaScript project, and it does what it should.

But my IDE shows that there is an unresolved function or method:但是我的 IDE 显示有一个未解决的 function 或方法:

未解决的功能或方法

I'm new to JavaScript and was wondering about this warning because it works.我是 JavaScript 的新手,我想知道这个警告,因为它有效。

So I searched and found this:所以我搜索并发现了这个:

The Kotlin/JS IR compiler is capable of generating TypeScript definitions from your Kotlin code. Kotlin/JS IR 编译器能够从您的 Kotlin 代码生成 TypeScript 定义。 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 JavaScript and TypeScript projects. 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 JavaScript and TypeScript projects. https://kotlinlang.org/docs/reference/js-ir-compiler.html#preview-generation-of-typescript-declaration-files-dts https://kotlinlang.org/docs/reference/js-ir-compiler.html#preview-generation-of-typescript-declaration-files-dts

I assume that I need these TypeScript definitions to tell my IDE that this method exists.我假设我需要这些 TypeScript 定义来告诉我的 IDE 这种方法存在。

Hours later, I got how to tell the Kotlin compiler(?) to generates this definition, but the warning is still there:-(几小时后,我知道如何告诉 Kotlin 编译器(?)生成这个定义,但警告仍然存在:-(

The generated shared.d.ts file looks like this:生成的shared.d.ts文件如下所示:

type Nullable<T> = T | null | undefined
export namespace api.model {
    class AuthResponse {
        // lots of code
    }
}
export namespace api {
    function requestToken(): kotlin.js.Promise<api.model.AuthResponse>;
}
export as namespace shared;

This is the generated package.json file:这是生成的package.json文件:

{
  "main": "kotlin/shared.js",
  "devDependencies": {
    "webpack": "4.44.1",
    "webpack-cli": "3.3.12",
    "source-map-loader": "1.0.1",
    "dukat": "0.5.8-rc.3"
  },
  "dependencies": {
    // kotlin related imports
  },
  "peerDependencies": {},
  "optionalDependencies": {},
  "bundledDependencies": [],
  "name": "shared",
  "version": "1.0.0"
}

In the JavaScript project, I added this line to the package.json file "shared": "file:..<path to the other package.json file>" . In the JavaScript project, I added this line to the package.json file "shared": "file:..<path to the other package.json file>" .

I also found this question How to use TypeScript declaration files alongside JavaScript , but maybe I'm not able to follow the answer, or it doesn't solve my problem.我还发现了这个问题How to use TypeScript 声明文件以及 JavaScript ,但也许我无法遵循答案,或者它不能解决我的问题。

Is there a way to tell the IDE that this method exists?有没有办法告诉 IDE 这种方法存在? Should this TypeScript declaration file solve it?这个 TypeScript 声明文件应该解决吗? And if yes, how do I use/import it?如果是,我该如何使用/导入它?

Simply speaking, typescript declaration files are used to allow typescript to understand JS.简单来说,就是使用typescript声明文件让typescript理解JS。 Not the other way around.不是反过来。 A .d.ts file is only useful in a TypeScript project, which allows TypeScript to do type-inference on a JavaScript module. .d.ts文件仅在 TypeScript 项目中有用,它允许 TypeScript 对 JavaScript 模块进行类型推断。

If you want to resolve the warning, then import the shared.js file in your JS project.如果您想解决警告,请在您的 JS 项目中导入shared.js文件。

The following works at least for IntelliJ IDEA Ultimate:以下至少适用于 IntelliJ IDEA Ultimate:

Adding "@types/shared": "file:../blh_shared/build/js/packages/shared" to the package.json file of my React project adds code completion in my IDE.添加"@types/shared": "file:../blh_shared/build/js/packages/shared"到我的 React 项目的package.json文件中添加代码完成在我的 Z581D6381F3F38E4BF364

The package.json file now looks like this: package.json文件现在如下所示:

{
  "name": "react_app",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "shared": "file:../blh_shared/build/js/packages/shared",
  },
  "devDependencies": {
    "@types/shared": "file:../blh_shared/build/js/packages/shared",
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 JavaScript 旁边使用 TypeScript 声明文件 - How to use TypeScript declaration files alongside JavaScript 如何在TypeScript中使用声明文件 - How to use declaration files in TypeScript 如何在单独的项目中使用自动生成的打字稿声明文件? - how to use auto generated typescript declaration files in separate project? 使用 JavaScript Class Mixins 与 TypeScript 声明文件 - Using JavaScript Class Mixins with TypeScript Declaration Files 如何在没有模块声明的情况下导入Typescript - How to import in Typescript without module declaration 在TypeScript中创建声明文件以使用JavaScript库 - Creating a declaration file in TypeScript to use a JavaScript library 当包有声明文件时,如何将 Javascript 库导入 Typescript - How do I import a Javascript library into Typescript when the package has a declaration file 如何从 JavaScript 文件中导入变量并在 TypeScript 中使用它? - How to import a variable from a JavaScript file and use it in TypeScript? 我如何才能将JavaScript声明打字稿文件中的static方法和实例方法与defineType区别开来? - How could I differentiate between static methods and instance methods in javascript declaration typescript files from definitelyTyped? 如何在JavaScript中导入Typescript类? - How to import a typescript class in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM