如何允许缺少.d.ts类型定义的模块? [英] How to allow modules that have missing .d.ts type definition?

查看:601
本文介绍了如何允许缺少.d.ts类型定义的模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些不受欢迎的模块,例如Dyojs-sha3,它们似乎没有任何类型.

I'm using some unpopular modules such as Dyo and js-sha3 which doesn't have any types, it seems.

我现在真的不在乎第三方库中的类型,我不想花很多时间来键入这些类型.我主要在服务器上使用它,以限制我的错误并使开发过程中的故障排除更加容易.

I don't really care right now about types in third-party libraries, I don't want to spend hours typing those. I mainly use it for the server to limit my mistakes and make troubleshooting easier while in development.

之前我有一个Cannot find module X错误,所以我在tsconfig.json文件中添加了"moduleResolution": "node".

I had a Cannot find module X error before so I've added: "moduleResolution": "node" in my tsconfig.json file.

如何使打字稿不抱怨节点模块缺少类型?

How can I make typescript not complaining about node modules missing types?

整个错误:

Could not find a declaration file for module 'dyo'. '/node_modules/dyo/dist/dyo.umd.js' implicitly has an 'any' type.
  Try `npm install @types/dyo` if it exists or add a new declaration (.d.ts) file containing `declare module 'dyo';` [7016]

我在网络的某个暗处的评论中找到了// @ts-nocheck,但是它似乎不起作用.

I found // @ts-nocheck in a comment in some dark place of the web but it doesn't seem to work.

我可以通过模块将其关闭吗?

Can I turn this off by modules?

推荐答案

您可以创建虚拟.d.ts定义,该定义允许从模块中导入任何内容-导入它会导致dyo具有any类型(确切的导入方式取决于esModuleInterop编译器设置,以及如何从dyo.umd.js导出内容.)

You can create dummy .d.ts definition which will allow to import anything from the module - importing it will result in dyo having any type (the exact way to import it depends on the esModuleInterop compiler setting, and on how the things are exported from dyo.umd.js).

  1. 在项目中某个位置创建一行文件dyo.d.ts,其中一行仅一行

declare module 'dyo';

  • 在引用该模块的文件中,在此行的顶部添加dyo.d.ts文件的适当路径:

  • in a file that references that module, add this line at the top, with appropriate path to dyo.d.ts file:

    /// <reference path='./dyo.d.ts' />
    

  • 2的替代方法是将dyo.d.ts添加到tsconfig.json文件的编译中包含的文件列表中:

    An alternative to 2. is to add dyo.d.ts to the list of the files included in the compilation in tsconfig.json file:

    "files": [
        ... other files as necessary ...
    
        "./path/to/dyo.d.ts"
    ]
    

    这篇关于如何允许缺少.d.ts类型定义的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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