简体   繁体   中英

How to declare module public type definition for NPM in Typescript

I created a npm package ( https://github.com/d6u/observe-object-path ). It is written in Typescript. It has a build step to compile it down to ES5 so any JavaScript environment can consume it ( https://github.com/d6u/observe-object-path/blob/master/package.json#L39-L41 ).

  1. Since the module is already written in Typescript. Is it possible to automatically generate a .d.ts file for my package, so people using Typescript can use it with minimum or no configuration?

  2. If automatic generate is not possible, what's the best place to place the .d.ts file, under ts directory (which apparently is what Rx 4 is doing https://github.com/Reactive-Extensions/RxJS/tree/master/ts )?

To generate .d.ts files for your project you must enable the declaration option in the compiler configuration.

An example with tsconfig.json .

{
    "compilerOptions": {
        "module": "system",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": false,
        "experimentalDecorators": true,
        "declaration": true
    },
    "exclude": [
        "node_modules"
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM