Create a Node package using Typescript 1.5 and generate a declaration file

I am new to Javascript, Node and Typescript and am trying to use them.


TL; DR

  • How to properly build a Node package using Typescript?
  • How do I properly create a declaration file that conforms to the Node API in one nice file?

Here's my story:

I would like to use Typescript to encode a javascript library in Node.js format. And finally, I would like to create an appropriate declaration file to make it available for future Typescript development. I am using gulp-typescript

to compile them.

After hours of research and error ...

Here's what I did:

foo.ts

export class Foo {
    private id: number;
    constructor() {
        this.id = 0;
    }
}

      

impl.ts

import foo = require('foo');

export function make(): boolean {
    var f = new foo.Foo();
    return true;
}

      

main.ts

declare module 'test' {
    export import impl = require('impl');
}

module.exports = {
    impl: require('impl')
};

      

gulpfile.js

var gulp = require('gulp')
  , ts = require('gulp-typescript');    

var TS_FILES = 'src/**/*.ts';    

gulp.task('default', ['build_node']);    

// Compile the node package from Typescript source code
gulp.task('build_node', function() {
  var project = ts.createProject({
    declarationFiles: true,
    emitDecoratorMetadata: true,
    module: 'commonjs',
    noEmitError: true,
    noImplicitAny: true,
    removeComments: true,
    target: 'ES5'
  });    

  var tsResult = gulp.src(TS_FILES)
  .pipe(ts(project));    

  tsResult.js
  .pipe(gulp.dest('build'));    

  tsResult.dts
  .pipe(gulp.dest('definition'));
});

      

Here's what I have:

  • 3 declaration files:

foo.d.ts

export declare class Foo {
    private id;
    constructor();
}

      

impl.d.ts

export declare function make(): boolean;

      

main.d.ts

declare module 'test' {
    export import impl = require('impl');
}

      

  • 3 javascript files:

foo.js

and they impl.js

look fine. AND...

main.js

module.exports = {
    impl: require('impl')
};

      

I don't even know if it works or not, but of course it doesn't look right ... I mean, I really think I messed up and this is not the correct way to create a Node package and its declaration file.

The reasons?

When I look at the DefinitelyTyped declaration file, most of them are in the same file with each declaration export

, as shown below:

declare module 'test' {
    export function make(): boolean;
    ...
}

      

And let the Node package expose its API like this:

module.exports = {
    version: '1.0',
    impl: require('impl');
}

      

I have read a hundred posts but nothing similar generates what I want.

Could you please explain to me:

  • How to properly build a Node package using Typescript?
  • How do I properly create a declaration file that conforms to the Node API in one nice file?

Edit: I am not using export =

it because I want to be able to declare and export multiple things in one file. Example: having a class helper and constructor in the same file.

+3


source to share


2 answers


This is how I can create something that works

project



  • Build js lib separately as is.
    • Remove all tags ///<reference ...

      from output
  • Assembly type definition file from template
    • Build definition files as is
    • Combine them all into one file
    • Remove all tags ///<reference ...

      and import

      from output
    • Insert output record into index.tmpl.d.ts

      file template
    • Replace declare export ...

      withexport ...

+1


source


There are many ways to do this while there is no official support yet. You can track the official support here: https://github.com/Microsoft/TypeScript/issues/2338

However, I am using https://github.com/TypeStrong/atom-typescript#packagejson-support



Sample NPM project: https://github.com/basarat/ts-npm-module
Sample project using NPM project: https://github.com/basarat/ts-npm-module-consume

+2


source







All Articles