简体   繁体   English

如何使用新的@types包键入TypeScript?

[英]How to use the new @types packages for TypeScript typings?

I've started to migrate a large existing, namespace and <reference> based TypeScript application to use modules and the @types NPM repository. 我已经开始迁移现有的大型,基于namespace<reference>的TypeScript应用程序,以使用modules@types NPM存储库。 However, I'm running into problems in every second. 但是,我每秒都遇到问题。

  1. Lots of libraries don't even have typings there. 许多图书馆甚至都没有打字。
  2. Lots of libraries have completely outdated typings there. 许多图书馆在那里的打字完全过时了。
  3. Lots of libraries have a global typings file, resulting that I cannot import it because TS complains that it's not a module. 许多库都有global文件,导致我无法import它,因为TS抱怨它不是模块。
  4. When a library has a module shaped typing file correctly included in it's NPM package (like moment.js ), TS still seems to be unable to find it, and errors the line import * as moment from 'moment' saying module is not found. 当一个库的NPM包中正确包含了一个module形状的类型文件(例如moment.js )时,TS似乎仍然找不到它,并且import * as moment from 'moment'说模块未找到而导致行import * as moment from 'moment'错误。

What is the current state of this technique? 该技术的当前状态是什么? As I see it at first glance, it seems to be far from being production ready. 乍看之下,它似乎还远远没有做好生产准备。

What are the hacks and techniques to overcome these issues? 解决这些问题的技巧和技巧是什么?

I'm targetting es5 and using es6 as modules configuration. 我的目标是es5并使用es6作为modules配置。 Can this be the problem? 这可能是问题吗? As far as I know TS supports the es6 module syntax for es5 output as well. 据我所知,TS也支持es5输出的es6模块语法。

The current way things are done is definitely production-ready; 当前完成工作的方式绝对可以用于生产。 however, there's a few things which aren't obvious and that isn't your fault. 但是,有些事情并不明显,这也不是您的错。

Let me try to answer your questions one-by-one. 让我尝试一个接一个地回答您的问题。

  1. Lots of libraries don't even have typings there. 许多图书馆甚至都没有打字。

In TypeScript 2.1, as long as you have a package installed in node_modules and you don't have noImplicitAny turned on, you can just import whatever you want. 在TypeScript 2.1中,只要您在node_modules安装了一个包并且没有打开noImplicitAny ,就可以导入所需的任何内容。

If you do want to use noImplicitAny (which I would recommend for any project that you expect to grow over time), you can just always create a declarations.d.ts in your project source folder that declares the modules as you need: 如果你想使用noImplicitAny (这我会建议您预计随着时间的推移长大任何项目),你可以随时创建declarations.d.ts项目源文件夹,因为你需要声明的模块:

// do anything you want when you import `"foo"` now.
declare module "foo";

// can only access the `hello` export when importing `"bar"`.
declare module "bar" {
    export var hello;
}
  1. Lots of libraries have completely outdated typings there. 许多图书馆在那里的打字完全过时了。

You should definitely feel free to send a pull request to DefinitelyTyped, but if you're really strapped for time, you can just use the approach I gave for point (1). 您绝对可以随意向DefinitelyTyped发送拉取请求,但是如果您真的很费时间,则可以使用我针对要点(1)给出的方法。

  1. Lots of libraries have a global typings file, resulting that I cannot import it because TS complains that it's not a module. 许多库都有global文件,导致我无法import它,因为TS抱怨它不是模块。

If you need to use a global declaration file from DefinitelyTyped, and it's not automatically included in your project, you may have to add it to the types field in your tsconfig.json . 如果您需要使用DefinitelyTyped中的全局声明文件,并且该文件未自动包含在您的项目中,则可能需要将其添加到tsconfig.jsontypes字段中。 See my other answer here , but the crux of it is that if you need to include global declarations for the foo and bar packages, and you have @types/foo and @types/bar installed, you can write the following in your tsconfig.json . 在这里看到我的其他答案,但是关键是如果您需要包括foobar软件包的全局声明,并且已经安装了@types/foo@types/bar ,则可以在tsconfig.json编写以下tsconfig.json

{
    "compilerOptions": {
        "types": ["foo", "bar"]
    }
}
  1. When a library has a module shaped typing file correctly included in it's NPM package (like moment.js ), TS still seems to be unable to find it, and errors the line import * as moment from 'moment' saying module is not found. 当一个库的NPM包中正确包含了一个module形状的类型文件(例如moment.js )时,TS似乎仍然找不到它,并且import * as moment from 'moment'说模块未找到而导致行import * as moment from 'moment'错误。

I believe that this has to do with the fact that you're targetting ES6. 我认为这与您以ES6为目标有关。 Try changing your "moduleResolution" option to "node" in your tsconfig.json . 尝试在tsconfig.json "node" tsconfig.json "moduleResolution"选项更改为"node"

{
    "compilerOptions": {
        "moduleResolution": "node"
    }
}

Sorry that that is a gotcha. 抱歉,这是一个陷阱。 The rationale is that the only module loading system that actually has this resolution semantics is CommonJS (the strategy that Node uses). 基本原理是,唯一具有这种解析语义的模块加载系统是CommonJS(Node使用的策略)。 I agree that that could be improved. 我同意可以改善这一点。

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

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