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

2

6

I'm writing a TypeScript definition file for an existing node library which use building node module like http and events.EventEmitter as a parameter.

my question is how can I write a definition file for this library? I have tried to copy these modules from node.d.ts into my own definition file, but I don't think this is a good idea.

Eelworm answered 23/3, 2016 at 15:33 Comment(2)
Not sure what do you mean with use node module as parameter Are you refering to import definitions from another .d.ts file?Gwyngwyneth
the library is written in vanilla js, and there is a function take events.EventEmtter as parameter like function(emitter) when I want to write a d.ts file for this library, I don't know how to import EventEmitter from node.d.ts, even I can import the events.EventEmtter from node.d.ts, how can I ask user to download the node.d.ts automatically?Eelworm
G
7

Your module should include it's own node.d.ts file among your .d.ts file (let's call it my_awesome_lib.d.ts)

In your .d.ts file you may include the necessary types as following:

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
}
Gwyngwyneth answered 23/3, 2016 at 16:3 Comment(0)
L
2

Use the Typings tool with a typings.json file to manage TypeScript definition dependencies.

See that project's 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 <[email protected]>",
  "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
Leighannleighland answered 23/3, 2016 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.