typescript - 如何声明一个模块导出任何内容,无论它导出什么?

标签 typescript

我的 UI 包没有类型定义文件。当我在 TypeScript 项目中使用它时

import { Cell } from 'zarm';

TSC 将抛出错误:

Could not find a declaration file for module 'zarm'.

我创建了一个 global.d.ts 文件,并尝试声明 zarm 模块来导出任何内容。

declare module 'zarm' {
  const x: any;
  export = x;
}

TSC 抛出:

Module '"zarm"' has no exported member 'Cell'.

zarm模块中有很多组件,我只想声明zarm模块来导出any。不要关心它到底导出什么。

最佳答案

您可以使用Shorthand ambient modules .

If you don’t want to take the time to write out declarations before using a new module, you can use a shorthand declaration to get started quickly.

只需在 global.d.ts 类型定义文件中为现有 JavaScript 包 zarm 声明模块即可:

declare module 'zarm';

它可以让您导入 any 类型的 zarm 模块。这样我就可以从模块导入任何组件,而无需使用 //@ts-ignore 注释来跳过 TSC 类型检查。

import { Button, Popup } from 'zarm';

关于typescript - 如何声明一个模块导出任何内容,无论它导出什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68740452/

相关文章:

javascript - 调用 jquery $(html :string) from TypeScript

javascript - 每个 React 类方法的 "Missing return type on function"

reactjs - 类型中缺少不应手动传递给子组件的属性

c# - C# 任务的 Angular 对应项

html - 如何在 ionic 3 中显示视频列表

typescript - react Redux TS : TypeScript complains about a connect() call

javascript - chrome 未定义 - Mocha 和 Typescript

compilation - Typescript 1.5 中的 'var' 和 'let'

reactjs - 如何使用 RouteComponentProps 测试 React 组件?

javascript - Typescript async/await 和 angular $q 服务