无类型 npm 模块的 TypeScript 自定义声明文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42693836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:19:38  来源:igfitidea点击:

TypeScript custom declaration files for untyped npm modules

reactjstypescriptecmascript-6visual-studio-codetypescript2.0

提问by tugberk

I am consuming a React component called shiitakefrom npm into my project where I use TypeScript. That library does not have TypeScript declarations so I thought I would write one. The declaration file looks like below (it may not be complete but don't worry about it too much):

我正在将一个名为shiitake的 React 组件从 npm消耗到我使用 TypeScript 的项目中。该库没有 TypeScript 声明,所以我想我会写一个。声明文件如下所示(可能不完整但不要太担心):

import * as React from 'react';

declare module 'shiitake' {

    export interface ShiitakeProps {
        lines: number;
    }

    export default class Shiitake extends React.Component<ShiitakeProps, any> { 
    }
}

I have put this inside ./typings/shiitake.d.tsfile and on VS Code, I am seeing the below error:

我已将其放入./typings/shiitake.d.ts文件中,并在 VS Code 上看到以下错误:

[ts] Invalid module name in augmentation. Module 'shiitake' resolves to an untyped module at 'd:/dev/foo/foobar.foo.Client.Web/node_modules/shiitake/dist/index.js', which cannot be augmented.

[ts] 扩充中的模块名称无效。模块 'shiitake' 解析为位于 'd:/dev/foo/foobar.foo.Client.Web/node_modules/shiitake/dist/index.js' 的无类型模块,无法扩充。

On the consumption side, I am still getting the same error even if with the above declaration (since I have noImplicitAnycompiler switch turned on):

在消费方面,即使使用上述声明(因为我noImplicitAny打开了编译器开关),我仍然遇到相同的错误:

/// <reference path="../../../../typings/shiitake.d.ts" />
import * as React from 'react';
import Shiitake from 'shiitake';

[ts] Could not find a declaration file for module 'shiitake'. 'd:/dev/foo/foobar.foo.Client.Web/node_modules/shiitake/dist/index.js' implicitly has an 'any' type.

[ts] 找不到模块“shiitake”的声明文件。'd:/dev/foo/foobar.foo.Client.Web/node_modules/shiitake/dist/index.js' 隐式具有 'any' 类型。

The standard why of acquiring declaration files for this type of modules is through @types/wayand it works well. However, I cannot get the custom typings work. Any thoughts?

获取此类模块的声明文件的标准原因是通过@types/方式,并且效果很好。但是,我无法使自定义类型工作。有什么想法吗?

回答by mohamed hegazy

The declaration declare module 'shiitake';should be in a global scope. i.e. a top level declaration in a non module (where a module is a file with at least one top level importor export).

声明declare module 'shiitake';应该在全局范围内。即非模块中的顶级声明(其中模块是具有至少一个顶级import或 的文件export)。

A declaration of the form declare module '...' { }in a module is an augmentation. For more details see Typescript Module Augmentation.

declare module '...' { }模块中的表单声明是一种扩充。有关更多详细信息,请参阅Typescript 模块增强

So you want this file to look like:

所以你希望这个文件看起来像:

declare module 'shiitake' {

    import * as React from 'react';

    export interface ShiitakeProps {
        lines: number;
    }

    export default class Shiitake extends React.Component<ShiitakeProps, any> { 
    }
}

回答by Ed Staub

In my experience, a definition file will fail in this way if it has any exports or importsdeclared outside the module definition. If you use an IDE with auto-import, be warned!

根据我的经验,如果定义文件在模块定义之外声明了任何导出或导入,那么它就会以这种方式失败。如果您使用具有自动导入功能的 IDE,请注意!

回答by Sergey Dryganets

I had the same problem today.

我今天遇到了同样的问题。

Turns out I paste exported interfaces outside of the module declaration. The typings file I used as a template had private interfaces at the end of the file.

结果我在模块声明之外粘贴了导出的接口。我用作模板的打字文件在文件末尾有私有接口。

So my exported interfaces were declared outside of the module declaration. This is drives typescript compiler nuts.

所以我导出的接口是在模块声明之外声明的。这是驱动打字稿编译器的坚果。

Once I moved the interfaces into the module boundaries everything got fixed.

一旦我将接口移动到模块边界,一切都得到了修复。