如何从窗口获取lodash?

时间:2018-07-24 11:20:10

标签: typescript lodash typescript-typings

在纯JS中,我有代码

const { _ } = window;

现在我将项目迁移到Typescript 2.8,这会导致错误:

  

错误TS2459:类型“窗口”没有属性“ _”,也没有字符串索引签名。

tsconfig:

{
  'allowJs': true,
  'allowSyntheticDefaultImports': true,
  "target": "es2017",
  "baseUrl": "src",
  'resolveJsonModule': true,
  'noImplicitAny': false
}

PS:我无法通过代码导入使用lodash。

1 个答案:

答案 0 :(得分:0)

要使用打字稿在窗口上将lodash标记为“ _”,您可能需要执行以下操作:

import {LoDashStatic} from 'lodash'   // import the library
declare let window: {                 // declare the type for window
  _: LoDashStatic;                    // (we'd add jquery like so too)
}     
const {_} = window;                   // create the convenient constant

_.range(1, 3);                         // works now w/o compile error
相关问题