【问题标题】:Typescript shows error when importing something导入内容时打字稿显示错误
【发布时间】:2018-03-19 02:30:42
【问题描述】:

为什么使用import会报错?

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    你还没有在你的文件中导入 lodash:

    import _ from 'lodash';
    

    【讨论】:

    • 但我不想导入 loadash,我会将它包含在我的 html 页面中。我只想要 typescript 显示没有错误。有没有办法这样做?
    【解决方案2】:

    但我不想导入 loadash,我会将它包含在我的 html 页面中。

    如果你已经在 html 页面中导入了“lodash”,你可以在你的 typescript 源代码中使用 declare 关键字,如下所示:

    declare var _: any;
    
    _.map([1, 2, 3], function(el: number, index: number) {
    // ...
    });
    

    declare 关键字用于ambient declarations,您要在其中定义可能不是源自 TypeScript 文件的变量。

    -- 编辑--

    search 之后,我发现“lodash”类型定义可在以下位置找到: https://www.npmjs.com/package/@types/lodash

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2021-05-03
      • 2017-10-10
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多