react ts could not find a declaration file for module

在react中使用TS导致类型无法加载的问题,会抛出could not find a declaration file for module的错误

1. 导致这个错误的原因是

js库无法在Ts中正常加载

2. 解决方法

第一种方法

一般可以通过@types/***加载module

第二种方法

毕竟不是所有的module都提供ts版,建议采用第二种方法:

在src的根目录下新建一个文件夹,以.d.ts为结尾

 在里面声明自己使用的js的module

declare module '@jiaminghi/data-view-react' {
import dataV from '@jiaminghi/data-view-react'
export default dataV
}
在你的项目中引用
import FullScreenContainer from "@jiaminghi/data-view-react";
 
原文地址:https://www.cnblogs.com/youran-he/p/15042909.html