如何编写和使用DefinitelyTyped中不存在的自定义声明文件? [英] How can I write and use custom declaration files that don't exist on DefinitelyTyped?

查看:142
本文介绍了如何编写和使用DefinitelyTyped中不存在的自定义声明文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个名为 foo 的npm包,它在DefinitelyTyped上不存在。换句话说, @ types / foo 不存在(或者可能已过期!)

I'm using an npm package called foo that doesn't exist on DefinitelyTyped. In other words, @types/foo doesn't exist (or is potentially out of date!)

I我希望仍能在更严格的设置下使用它,比如 noImplicitAny ,所以我需要自己编写自定义定义文件。最后我想向DefinitelyTyped发送一个pull请求,这样这个文件对我项目之外的其他人都有用。

I'd like to still be able to consume it under stricter settings like noImplicitAny, so I need to write the custom definition files myself. Eventually I would like to send a pull request to DefinitelyTyped so that this file can be useful to others outside of my project.

有一些简单的解决方案,比如创建一个名为的全局文件 ./ src / types.d.ts 我可以写下以下内容

There are easy solutions like creating a global file named ./src/types.d.ts where I can write the following

declare module "foo" {
    export function hello(): void;
    export function world(): void;
}

但如果我使用该语法,我可能需要重写我的模块当我将它提交给DefinitelyTyped。

But if I use that syntax, I'll potentially need to rewrite my module when I submit it to DefinitelyTyped.

如何构建我的项目以便我可以轻松地创作然后将本地.d.ts文件发送到DefinitelyTyped?

How can I structure my project so that I can easily author and then send local .d.ts files to DefinitelyTyped?

推荐答案

我会一步一步地解释这个问题,所以这可能看起来很冗长,但按照给出的说明应该只需要几分钟。事实上,这是你可以在bash或PowerShell中运行的简短版本!

I'll do this step-by-step with explanations so this may seem lengthy, but following the instructions given should take just a few minutes. In fact, here's the short version you can just run in either bash or PowerShell!

mkdir -p ./local-types/foo
cd ./local-types/foo
npm init --scope types --yes
echo "export function hello(): void; export function world(): void" > ./index.d.ts
cd ../..
npm install ./local-types/foo



背景



让我们假设以下项目结构:

Background

Let's imagine the following project structure:

proj/
├─ tsconfig.json
└─ src/
   └─ ...



在项目的根目录下创建一个本地类型文件夹。



无论它叫什么都没关系,但是我们称之为 local-types 。一旦掌握了我们在这里所做的事情,你就可以把它改成你想要的任何东西。

Create a local-types folder at the root of your project.

Doesn't matter what it's called, but we'll call it local-types. You can change it to whatever you want once you have a good grasp on what we've done here.

proj/
├─ tsconfig.json
├─ local-types/
└─ src/
   └─ ...




在这个例子之外的大多数情况下,我可能只是将这个类型命名为



在本地类型目录中创建一个新包



因为你正在尝试要导入名为 foo 的模块,我们将使用索引创建名为 foo 的文件夹.d.ts

Create a new package in your local types directory

Since you're trying to import a module named foo, we'll create a folder named foo with an index.d.ts.

// local-types/foo/index.d.ts

export function hello(): void;

export function world(): void;

我们还希望通过创建package.json来使这个为npm包:

We'll also want to make this an npm package by creating a package.json:

cd local-types/foo
npm init --scope types --yes

此时,您的项目应如下所示:

At this point, your project should look like this:

proj/
├─ tsconfig.json
├─ local-types/
|  └─ foo/
|     └─ index.d.ts
|     └─ package.json
└─ src/
   └─ ...

您现在应该可以从 src 中的任何文件导入 foo

You should now be able to just import foo from any file within src.

import { hello, world } from "foo";

hello();
world();

请注意,您可能没有<$ c $的单入口套餐C> index.d.ts 。在这种情况下,您将需要模仿在npm上发布的包结构。

Keep in mind, you may not have a single-entry-point package with an index.d.ts. In that case, you'll want to mimick the package structure as it's been published on npm.

如果您正在编写库,您可能希望在依赖项中使用它,但应用程序可能希望使用devDependencies。只需添加文件:./ local-types / foo 作为的版本@ types / foo

If you're writing a library, you'll likely want this in your dependencies, but apps might want to use devDependencies. Simply add file:./local-types/foo as the version for @types/foo:

"dependencies": {
    "@types/foo": "file:local-types/foo"
}



发布到DefinitelyTyped



你的 .d.ts 文件完整且有用吗?考虑编写一些测试并向DefinitelyTyped发送拉取请求,以便其他人可以从您已完成的工作中受益!

Publish to DefinitelyTyped

Are your .d.ts files complete and useful? Consider writing some tests and sending a pull request to DefinitelyTyped so that others can benefit from the work you've done!

如果您能够导入那些没有的包在运行时出现问题,你已经有了适当的包结构。

If you're able to import those packages with no problems at runtime, you'll already have the appropriate package structure.


  • 不要在任何这些本地类型包中使用声明模块...语法。


    • ...除非您尝试编写模块扩充。但你可能不是。

    • Don't use the declare module "..." syntax within any of these local type packages.
      • ...unless you're trying to write a module augmentation. But you probably aren't.

      这篇关于如何编写和使用DefinitelyTyped中不存在的自定义声明文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆