Typescript: Extending an external module

Created on 28 Nov 2014  ·  3Comments  ·  Source: microsoft/TypeScript

I'm trying to extend an external node module:

// a) Use an import?
import System.Fs.Node = require('fs');

// b) Hackery? 
var System = System || {}
var System.Fs = System.Fs || {}
var System.Fs.Node = require('fs')

module System.Fs.Node {

    var path = require('path');

    interface IfOptions_mkdir {
        mode?: number
        recursive: boolean
    }

    export function mkdirSync($path: string, $options: IfOptions_mkdir|number) {
        try {
...

Any thoughts on how I might/should be extending an external module?

Question

All 3 comments

I think this could work, wins points for readability:

module System.Fs.Node {
    export * from "fs";

    export function mkdirSync($path: string, $options: IfOptions_mkdir|number) {
     //...
    }
  }

You can do this today:

In a separate .d.ts file:

declare module "fs" {
  stuff: number;
}

In your monkeypatcher:

/// reference the above .d.ts file
import fs = require('fs');
fs.stuff = 3;

I don't think we're going to add extra syntax for this since it's (hopefully?) not a common scenario.

FWIW this no longer works when using node resolution and "proper external modules". The ambient module simply overrides any node-resolved one.

Was this page helpful?
0 / 5 - 0 ratings