2

I'm confused when creating declaration files (d.ts).

For example, I created a NPM package "a" (one CommonJS module index.ts):

export interface IPoint {
    x: number;
    y: number;
}

export default function sub(one: IPoint, two: IPoint): IPoint {
    return {
        x: one.x - two.x,
        y: one.y - two.y
    };    
}

Compile it and generate a.d.ts:

export interface IPoint {
    x: number;
    y: number;
}
export default function sub(one: IPoint, two: IPoint): IPoint;

As I understand the compiler can not generate valid d.ts for CommonJS. Must use utilities as dts-generator or to wrap manually:

declare module "a" 
{
    export interface IPoint {
        x: number;
        y: number;
    }
    export default function sub(one: IPoint, two: IPoint): IPoint;
}

Ok. Now I am doing the package "b" (which depends on "a"):

/// <reference path="node_modules/a/a.d.ts" />

import sub from "a"
import { IPoint } from "a"
export { IPoint }

export default function distance(one: IPoint, two: IPoint): number {
    var s = sub(one, two);
    return Math.sqrt(s.x * s.x + s.y * s.y);
}

Ok, it work. Now a want a package "c" that depends on "b" (and so on).

  1. How to specify the dependence in the module "b" (link to "a.d.ts")? Trying to specify in "node_modules"? Or copy "a.d.ts" to "/typings/" directory in the "b" package? And then copy "a.d.ts" and "b.d.ts" to "/typings/" in the "c" package (and so on)?

  2. What is the point from sections "typings" in "package.json"? I write in "b/package.json":

    "typings": "./b.d.ts"

And get the error when compiling "c":

Exported external package typings file 'node_modules/b/b.d.ts' is not a module.
  1. How to create d.ts files for CommonJS module? So that do not write manually "declare module", "reference" and etc.

1 Answer 1

7

As I understand the compiler can not generate valid d.ts for CommonJS. Must use utilities as dts-generator or to wrap manually:

Actually no (since 1.6).

Simply compile module a passing the declaration flag to tsc. The transpiler will generate the declaration files for you.

You are right that these files are not external definition file, but internal definition files

however

Module b will be able to automatically find and use these files if, in the package.json of module a, the typings entry points to the generated index.d.ts file

To recap:

Module a:

  • compîle with the declaration flag
  • update the typings entry to point to the generated .d.ts file

Module b:

  • simply import * as moduleA from 'a' or import {IPoint} from module 'a'

No clumsy ///<reference= or hand-made typings

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, so simple :) Thank you, Bruno! But, if the library contains many modules? As "lodash" for example. I can include the particular module: let toArray = require("lodash/lang/toArray"); How to specify d.ts for nested modules? I received the following: - I have index.ts and submodule.ts - Compiled and created index.d.ts and submodule.d.ts - It working! :) - But I cannot move d.ts files to separate directory from the root. - I write 'typings: "./typings/my-module.d.ts"' and it working for the main module, but nested doesn't resolve. It can be fixed?
Long comment ;) For "pure" js libraries, get the definition/typings from definitelyTyped and reference them in the files section of tsconfig.json, see this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.