如何使用打字稿中的npm模块? [英] How to consume npm modules from typescript?

查看:88
本文介绍了如何使用打字稿中的npm模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打字稿.在hello世界阶段它工作正常.我现在正在尝试使用npm模块:

I'm giving a shot at typescript. It works fine at the hello world stage. I'm now trying to use a npm module :

index.ts =

import _ = require('lodash')

console.log(_.toUpper('Hello, world !'))

这不起作用:

  • tsc index.ts-> Cannot find module 'lodash'. (2307)
  • node-ts index.js-> Cannot find module 'lodash'. (2307)
  • tsc index.ts -> Cannot find module 'lodash'. (2307)
  • node-ts index.js -> Cannot find module 'lodash'. (2307)

查看打字稿文档和Google并没有帮助.其他S/O问题未得到解答(此处此处)或无关.

Looking at typescript documentation and in google didn't help. Other S/O questions are either unanswered (here and here) or unrelated.

元素:

  • 打字稿1.8最新
  • 是的,lodash已安装npm i --save lodash,并且存在于我的文件系统中(已选中)
  • 我也做了typings i --save lodash
  • 变体import * as _ from 'lodash'const _ = require('lodash')也不起作用
  • 我尝试按照其他答案"moduleResolution": "node""module": "commonjs"中的建议调整tsconfig.json选项,但仍然无法正常工作
  • typescript 1.8 latest
  • Yes, lodash is installed npm i --save lodash and exists in my filesystem (checked)
  • I also did typings i --save lodash
  • variants import * as _ from 'lodash' or const _ = require('lodash') don't work either
  • I tried tweaking tsconfig.json options as suggested in other answers "moduleResolution": "node" and "module": "commonjs" as suggested in some answers, still doesn't work

我们如何在打字稿中使用npm包?

How do we consume a npm package in typescript ??

推荐答案

[2018/12]新的,最新的,对我在2016年提出的问题的回答,尽管有答案过时.

长话短说,TypeScript需要有关包代码的类型信息(又称"

Long story short, TypeScript requires type informations about your package's code (a.k.a. "type declaration files" a.k.a. "typings") and rightfully tells you that you would otherwise be losing the whole point of TypeScript. There are several solutions to provide them or opt out of them, listed here in order of best practice:

解决方案0 :该模块已提供了键入内容.如果其package.json包含以下行:

Solution 0: the module already provides the typings. If its package.json contains a line like this:

"typings": "dist/index.d.ts",

它已经启用了TypeScript.如果您正在阅读此页面,则很可能不是这种情况,所以让我们继续...

it is already TypeScript-enabled. It's most likely not the case if you are reading this page, so let's continue...

解决方案1 ​​:使用来自 DefinitelyTyped 的社区贡献类型.对于模块"foo",请尝试以下操作:

Solution 1: use community-contributed typings from DefinitelyTyped. For a module "foo", try this:

npm add -D @types/foo

如果有效,请中奖!现在,您可以进行输入并可以使用您的模块了.如果npm抱怨找不到模块@ types/foo,让我们继续...

if it works, jackpot! You now have the typings and can use your module. If npm complains that it can't find the module @types/foo, let's continue...

解决方案2 :提供有关此模块的自定义类型. (可以选择零努力)

Solution 2: provide custom typings about this module. (with an option to do zero effort)

  1. 在项目的根目录创建一个名为"typings-custom"的文件夹
  2. 在您的tsconfig.json中引用此文件夹的内容:

"include": [
    "./typings-custom/**/*.ts"
]

  1. 使用以下确切名称创建文件:foo.d.ts [foo =模块名称],内容如下:

declare module 'foo'

您的TypeScript代码现在应该编译,尽管没有类型信息(TypeScript考虑类型为"any"的foo模块).

Your TypeScript code should now compile, albeit with NO type information (TypeScript consider the foo module of type "any").

您还可以尝试查看官方文档和/或来自 DefinitelyTyped 的示例.如果您愿意,可以考虑将您的类型直接贡献给模块(如果模块作者接受,则为解决方案0),或者直接贡献给DefinitelyTyped(解决方案1)

You can also attempt to write the type information yourself, looking at the official doc and/or at examples from DefinitelyTyped. If you do, think of contributing your typings either directly into the module (solution 0, if the module author accepts) or into DefinitelyTyped (solution 1)

这篇关于如何使用打字稿中的npm模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆