5.3.11 • Published 5 days ago

typenode v5.3.11

Weekly downloads
3
License
MIT
Repository
-
Last release
5 days ago

TypeNode

npm Travis

TypeScript 1.5 declarations for Node.js 0.12.

Why?

There already exists a set of Node.js type declarations in DefinitelyTyped. Why use TypeNode over those?

  • Available directly on npm
  • Focus on the Node.js standard library
  • Split into files corresponding to Node.js standard libraries
  • Up-to-date TypeScript (1.5)
  • Up-to-date Node.js (0.12)
  • Tested against Node.js documentation examples
  • Working subclass chaining methods (see CRTP)

Patterns

API

Only the documented, non-deprecated, Node.js standard library API is declared. As much as possible, the type declarations follow the order of the documentation. Each page of the API documentation corresponds to one .d.ts file.

Inter-dependencies

The node.d.ts file references all other declaration files. However, it is also possible to reference individual declaration files. For this reason, declaration files reference their inter-dependencies.

Internal Ambient Modules

There are several Node.js globals of types only exposed through ambient external modules (i.e import * as events from 'events';). In order to make these types available to globals, they are declared in ambient internal modules, prefixed with __. The globally available type is then aliased or subclassed in the ambient external module.

declare module __events {
  class EventEmitter {
    // ...
  }
}

declare module 'events' {
  class EventEmitter extends EventEmitter {}
}

CRTP

Many Node.js classes that are subclassed have chaining methods (i.e. methods that return this). TypeScript has no way to represent this as a return type, so the Curiously recurring template pattern is used to enable proper chaining return types on subclasses. The generic base class is prefixed with an underscore (inside a double-underscore-prefixed ambient internal module). The regular base class as well as subclasses then extend the prefixed class.

declare module __events {
  class _EventEmitter<This extends _EventEmitter<any>> {
    setMaxListeners(n: number): This;
    // ...
  }
}

declare module 'events' {
  class EventEmitter extends __events._EventEmitter<EventEmitter> {}
}

declare module 'stream' {
  class Readable extends __events._EventEmitter<Readable> {
    // ...
  }
}

Listener & Options Naming

Types of event listeners are named by the event with a Listener suffix.

type ExitListener = (code: number, signal: string) => void;

class ChildProcess {
  on(event: 'exit', listener: ExitListener): ChildProcess;
  // ...
}

Interfaces of options objects are named by the function or method with an Options suffix.

interface SpawnOptions {
  cwd?: string;
  // ...
}

function spawn(command: string, options?: SpawnOptions): ChildProcess;

Examples

All of the examples on an API documentation page are in the corresponding .ts in example. Each example is wrapped in a function to isolate scope. All requires are moved to the top of the file as ES6-style imports.

import * as assert from 'assert';

(function() {
  assert.throws(
    function() {
      throw new Error('Wrong value');
    },
    Error
  );
});

Occurrences of var are changed to let. Explicit types are added where needed. More significant changes to get the examples to compile under TypeScript should appear below the commented out original line.

Object structure examples should be assigned to a variable of the expected type.

(function() {
  let options: child_process.ExecOptions =
    { encoding: 'utf8',
      timeout: 0,
      maxBuffer: 200*1024,
      killSignal: 'SIGTERM',
      cwd: null,
      env: null };
});

License

Copyright © 2015, Curtis McEnroe curtis@cmcenroe.me

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

5.3.11

5 days ago

5.3.9

2 months ago

5.3.10

1 month ago

5.3.8

2 months ago

5.3.7

2 months ago

5.3.6

2 months ago

5.3.5

2 months ago

5.3.4

2 months ago

5.3.3

4 months ago

4.9.4-h

5 months ago

4.9.4-i

5 months ago

4.9.4-j

5 months ago

4.9.4

1 year ago

4.8.7

1 year ago

4.9.4-d

1 year ago

4.9.4-e

1 year ago

4.9.4-f

1 year ago

4.9.4-g

1 year ago

4.9.4-a

1 year ago

4.9.4-b

1 year ago

4.9.4-c

1 year ago

4.8.5

1 year ago

4.8.4

2 years ago

4.8.6

1 year ago

0.5.7

2 years ago

0.5.4

2 years ago

0.5.3

2 years ago

0.5.6

2 years ago

0.5.5

2 years ago

0.5.2

2 years ago

0.0.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.4.4

2 years ago

0.5.0

2 years ago

0.4.1

2 years ago

0.3.2

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.4.3

2 years ago

0.3.4

2 years ago

0.5.1

2 years ago

0.4.2

2 years ago

0.3.3

2 years ago

0.1.1

9 years ago

0.1.0

9 years ago