Mar*_*vos 5 javascript import require node.js typescript
我有一个网站的打字稿nodejs项目。我需要使用此特定nodejs模块中可用的功能:https ://www.npmjs.com/package/weather-js
该模块在其存储库中没有可用的.d.ts文件,在'@types'打字稿存储库中也没有可用的文件。我不需要此模块的类型定义,但打字稿不包含它,并且会产生错误。
无论我尝试采用哪种方式导入,我都可以
TS2304: Cannot find name 'require'.
Run Code Online (Sandbox Code Playgroud)
在构建控制台中,或
TypeError: qs.escape is not a function
Run Code Online (Sandbox Code Playgroud)
在浏览器开发者控制台中。
StackOverflow上的许多回复都提出了一些解决方案,这些解决方案要求存储库在2018年不再可用,或者可能会起作用,但对我来说仍然会产生错误
TypeError: qs.escape is not a function
Run Code Online (Sandbox Code Playgroud)
2018年在打字稿中使用/导入/要求/使用nodejs模块功能(没有可用的.d.ts定义)的正确方法是什么?
ale*_*ill 11
您可以添加一个虚拟定义以允许它导入,但无需实际输入模块内容(尽管也许您应该完整输入它们并提交给DefinitlyTyped,以便每个人也可以从类型安全中受益!)
天气js.d.ts
// declaring module will allow typescript to import the module
declare module 'weather-js' {
// typing module default export as `any` will allow you to access its members without compiler warning
var weatherJs: any;
export default weatherJs;
}
Run Code Online (Sandbox Code Playgroud)
你的代码.ts
import weather from 'weather-js'
weather.find({search: 'San Francisco, CA', degreeType: 'F'}, () => {})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3621 次 |
| 最近记录: |