【问题标题】:Convert typings defined with commonjs into typings without a module system将使用 commonjs 定义的类型转换为没有模块系统的类型
【发布时间】:2018-07-13 10:38:34
【问题描述】:

我正在从 VS 2013 + TS 1.8 迁移到 VS 2017 + TS 2.5

作为这项工作的一部分,我似乎必须为 react 等更新类型。

不幸的是,新的类型正在使用 commonjs :

https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-dom

旧类型没有使用任何模块系统。

有没有办法将 commonjs d.ts 转换为“无模块”d.ts?

【问题讨论】:

    标签: typescript typescript-typings definitelytyped


    【解决方案1】:

    react 的类型有 export as namespace React,这是特殊的 - 如果您的代码不使用任何模块系统,它允许您只使用全局变量React

    因此,例如,此代码与最新的 typescript@2.7.2@types/react@16.0.38@types/react-dom@16.0.4 一起编译(和类型检查)就好了:

    ReactDOM.render(
      <h1>Hello, world!</h1>,
      document.getElementById('root')
    );
    

    以下是有关 export as namespace 特殊功能的更多信息:https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#support-for-umd-module-definitions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-14
      • 2022-01-22
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 2013-11-27
      相关资源
      最近更新 更多