您在打字稿中使用的每个 JS 库都需要打字/定义文件吗? [英] Is it necessary to have typing/definiton files for every JS library you use in typescript?

查看:19
本文介绍了您在打字稿中使用的每个 JS 库都需要打字/定义文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你在打字稿中使用的每个 JS 库都需要打字吗?如果没有,那么如何消除错误并使用没有可用定义文件的库

Is it necessary to have typing for every JS library you use in typescript? if not, then how to get rid of errors and make use of a library which doesn't have available definition files

import { NotificationContainer, NotificationManager } from 'react-notifications';

找不到反应通知的类型

推荐答案

没有必要.对于您的示例,您创建了一个文件 react-notifications.d.ts(只要扩展名是 .d.ts,您可以随意命名它,但它会使一致地命名它是有意义的):

It's not necessary. For your example, you create a file react-notifications.d.ts (you can call it anything you like as long as the extension is .d.ts but it makes sense to name it consistently):

declare module "react-notifications" {

    const NotificationContainer: any;
    const NotificationManager: any;
}

这是一个接近最低限度的起点.但是您可以进一步改进那些 any 声明.他们是临时警察.

That's a near-minimal starting point. But you could go a little further and improve on those any declarations. They are a temporary cop-out.

这篇关于您在打字稿中使用的每个 JS 库都需要打字/定义文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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