简体   繁体   English

如何导入一个 js 模块。 (没有任何声明文件(d.ts))

[英]How to import a js Module as any. (Without any declaration file (d.ts))

So I want to import a js module in my ts app.所以我想在我的ts应用程序中导入一个js模块。

Is there a way to do this without making a d.ts file or if not how to declare it as any in the d.ts file?有没有办法在制作d.ts文件的情况下做到这一点或者如果没有,如何在d.ts文件中将其声明为any

I am currently just ignoring this error with //@ts-ignore.我目前只是使用 //@ts-ignore 忽略此错误。

Thanks!谢谢!

There are two cases here:这里有两种情况:

  • You are using ESM modules, and only have import(...) available您正在使用 ESM 模块,并且只有import(...)可用
const module = (await import('module')) as any;
  • You aren't using ESM modules, therefore you have access to require(...)您没有使用 ESM 模块,因此您可以访问require(...)
const module = require('module') as any;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建一个`d.ts`文件来导入一个非模块的外部npm库 - how to create a `d.ts` file to import an external npm lib that is not a module 如何在没有d.ts文件的情况下导入第三方软件包? - How can I import third party package without d.ts file? 如何创建一个导出现有模块的.d.ts文件? - How to create a .d.ts file that exports an existing module? 如何使用在 adts 文件中定义的类型的模块? - How to use a module with types defined in a .d.ts file? 从.d.ts文件导入打字稿 - Typescript import from .d.ts file Typescript导入一个React js组件,该组件由我自己编写并出现了d.ts文件。找不到模块:无法解析“ xxx”中的“ xxx” - Typescript import a React js component which d.ts file writed by myself and occured Module not found: Can't resolve 'xxx' in 'xxx' d.ts文件导出模块 - d.ts file export module 如何为内部 JavaScript 模块创建 .d.ts 文件 - How to create a .d.ts file for an internal JavaScript module 包含`declare module 'graphql/error/GraphQLError' 的声明(.d.ts)文件; 在 angular 项目的 index.d.ts 中 - declaration (.d.ts) file containing `declare module 'graphql/error/GraphQLError'; in index.d.ts in an angular project 引入类型以在带有声明模块的.d.ts文件中使用,而不使其成为模块文件 - Pulling in types for use in a .d.ts file with declare module without making it a module file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM