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

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

问题描述

我正在尝试打字稿.它在 hello world 阶段运行良好.我现在正在尝试使用 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 -> 找不到模块lodash".(2307)
  • node-ts index.js -> 找不到模块lodash".(2307)
  • tsc index.ts -> Cannot find module 'lodash'. (2307)
  • node-ts index.js -> Cannot find module 'lodash'. (2307)

查看打字稿文档和谷歌没有帮助.其他 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
  • variants import * as _ from 'lodash'const _ = require('lodash') 也不起作用
  • 我尝试按照其他答案中的建议调整 tsconfig.json 选项 "moduleResolution": "node""module": "commonjs" 正如某些答案中所建议的那样,还是不行
  • 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 需要关于包代码的类型信息(又名类型声明文件" aka "typings") 并正确地告诉您,否则您将失去 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,如果模块作者接受)或绝对类型(解决方案 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天全站免登陆