gpt4 book ai didi

typescript - 如何将 typescript 定义文件添加到 npm 包中?

转载 作者:搜寻专家 更新时间:2023-10-30 20:32:42 25 4
gpt4 key购买 nike

有没有办法将 typescript 定义(.d.ts 文件)直接添加到纯 javascript 项目(例如,在 package.json 中)。我找不到任何相关文档。

最佳答案

TypeScript Handbook中有一个页面将类型添加到 NPM 包。我将复制并粘贴到这里:

Typings for NPM Packages

The TypeScript compiler resolves Node module names by following the Node.js module resolution algorithm. TypeScript can also load typings that are bundled with npm packages. The compiler will try to discover typings for module "foo" using the following set of rules:

Try to load the package.json file located in the appropriate package folder (node_modules/foo/). If present,read the path to the typings file described in the "typings" field. For example, in the following package.json, the compiler will resolve the typings at node_modules/foo/lib/foo.d.ts

{
"name": "foo",
"author": "Vandelay Industries",
"version": "1.0.0",
"main": "./lib/foo.js",
"typings": "./lib/foo.d.ts"
}

Try to load a file named index.d.ts located in the package folder (node_modules/foo/) - this file should contain typings for the package.

The precise algorithm for module resolution can be found here.

Your definition files should

  • be.d.ts files
  • 写成外部模块
  • 不包含三斜线引用

The rationale is that typings should not bring new compilable items to the set of compiled files; otherwise actual implementation files in the package can be overwritten during compilation. Additionally, loading typings should not pollute global scope by bringing potentially conflicting entries from different version of the same library.

关于typescript - 如何将 typescript 定义文件添加到 npm 包中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37210059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com