如何编写依赖于另一个定义文件的 TypeScript 定义文件?

How can I write TypeScript definition files that depend on another definition file?

我正在为现有节点库编写 TypeScript 定义文件,该节点库使用 httpevents.EventEmitter 等构建节点模块作为参数。

我的问题 是如何为这个库编写定义文件?我试图将这些模块从 node.d.ts 复制到我自己的定义文件中,但我认为这不是一个好主意。

使用 Typings 工具和 typings.json 文件来管理 TypeScript 定义依赖项。

查看该项目的 FAQ

Start by creating a new typings.json file, then add dependencies as normal. When you publish to GitHub, locally, alongside your package (NPM or Bower) or even to your own website, someone else can reference it and use it.

{
  "name": "typings",
  "main": "path/to/definition.d.ts",
  "author": "Blake Embrey <hello@blakeembrey.com>",
  "description": "The TypeScript definition dependency manager",
  "dependencies": {}
}
  • main The entry point to the definition (canonical to "main" in NPM's package.json)
  • browser A string or map of paths to override when resolving (following the browser field specification)
  • ambient Denote that this definition must be installed as ambient
  • name The name of this definition
  • postmessage A message to emit to users after installation
  • version The semver range this definition is typed for
  • dependencies A map of dependencies that need installing
  • devDependencies A map of development dependencies that need installing
  • ambientDependencies A map of environment dependencies that may need installing
  • ambientDevDependencies A map of environment dev dependencies that may need installing

你的模块应该在你的 .d.ts 文件中包含它自己的 node.d.ts 文件(我们称之为 my_awesome_lib.d.ts

在您的 .d.ts 文件中,您可以包括以下必要的类型:

declare module 'my_awesome_lib' {
  import * as express from 'express'; // just as example
  import { EventEmitter } from 'events'; // here you go
  export function foo(EventEmitter e): boolean; // your function
}