简体   繁体   English

将Lodash与TypeScript和JSPM结合使用

[英]Using lodash with typescript and jspm

I am maybe doing something wrong but I cannot find what so any help would be greatly appreciated. 我可能做错了事,但找不到任何帮助,将不胜感激。 I am Using typescript 2 + jspm. 我正在使用打字稿2 + jspm。 I think I tried all the possibilities inside the tsconfig using typeRoots and types (adding the version number in the name of the type). 我想我使用typeRoots类型 (在版本名称中添加版本号)尝试了tsconfig内部的所有可能性。 My current config is the following and it is not working whereas I think it should... 我当前的配置如下,它不起作用,而我认为应该...

package.json package.json

  "jspm": {
    "dependencies": {
      "lodash": "npm:lodash@^4.17.4"
    },
    "devDependencies": {
      "@types/lodash": "npm:@types/lodash@^4.14.45",
      "babel": "npm:babel-core@^5.8.24",
      "babel-runtime": "npm:babel-runtime@^5.8.24",
      "core-js": "npm:core-js@^1.1.4",
      "systemjs": "npm:systemjs@^0.19.41"
    }
  }

tsconfig.json tsconfig.json

"typeRoots": [
  "jspm_packages/npm/@types"
]

Then compiler does not understand 然后编译器不明白

import * as _ from "lodash"

I get 我懂了

Cannot find module 'lodash'.

as suggested by the typescript doc https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html 根据打字稿文档https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html的建议

Now if I remove the import, the funny thing is that vcode is able to go find the merge method definition (F12) if I write the following line of code 现在,如果我删除导入,有趣的是,如果我编写以下代码行,则vcode能够找到合并方法定义(F12)

_.merge(a, b);

but compiler still complains that 但是编译器仍然抱怨

Identifier '_' must be imported from a module

Any idea ? 任何想法 ? :) :)

This is not really a solution but for now to avoid Typescript compiler complaining this is what I do 这不是真正的解决方案,但现在要避免Typescript编译器抱怨这是我要做的

declare const _: any;

I just remove that line when I want completion for methods signature 我想完成方法签名时就删除该行

Note that when I import lodash 请注意,当我导入lodash

 System.import('lodash').then((lodash) => {
     window._ = lodash;
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM