【问题标题】:create-react-app error import type * as PrettyFormat from './types' in pretty-formatcreate-react-app 错误 import type * as PrettyFormat from './types' in pretty-format
【发布时间】:2020-07-06 10:13:41
【问题描述】:

这是在节点 13.11.0 上运行 typescript 模板的全新 cra 安装,尝试运行 npm start 时出现错误

TypeScript error in D:/Projects/personal-site/node_modules/@types/testing-library__react/node_modules/pretty-format/build/index.d.ts(7,13):
'=' expected.  TS1005

     5 |  * LICENSE file in the root directory of this source tree.
     6 |  */
  >  7 | import type * as PrettyFormat from './types';
       |             ^
     8 | /**
     9 |  * Returns a presentation string of your `val` object
    10 |  * @param val any potential JavaScript object```

【问题讨论】:

    标签: reactjs typescript create-react-app


    【解决方案1】:

    更新

    感谢marcelo-waisman的帖子。

    将 Typescript 升级到 > 3.8.0 解决了这个问题。

    如果我们紧紧跟随documentation...

    yarn add typescript @types/node @types/react @types/react-dom @types/jest
    

    ...我们应该安装最新版本的 TypeScript,而不是可能仍低于 3.8.0 的本地全局版本。


    我发现了一些线索。也许值得打开一个问题。

    它与包pretty-format 及其在create-react-app 中的类型有关。 我们可以在这里找到来源:pretty-format/src/index.ts

    import style = require('ansi-styles');
    import type * as PrettyFormat from './types';
    

    从commit history我们可以看到前两天有一个相关的Pull Request,把这部分从...改成了...

    import * as PrettyFormat from './types';
    

    ...到...

    import type * as PrettyFormat from './types';
    

    这似乎是错误的原因。

    【讨论】:

      【解决方案2】:

      其实这个“导入类型”是Typescript 3.8引入的新语法 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-exports

      解决方案实际上非常简单(如果您愿意的话),只需将您的 typescript 升级到 3.8+

      【讨论】:

      • 是的,手动更新打字稿版本解决了这个问题。
      猜你喜欢
      • 2019-07-02
      • 2013-06-26
      • 2021-11-08
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 2020-05-11
      • 2021-09-19
      • 1970-01-01
      相关资源
      最近更新 更多