scripting - Automatically generating ambient module declarations -
given these 2 typescript files
api/token.ts
interface token { code: string } export default token
and index.ts
export * './api/token'
tsc 1.5 --declarations
switch generate 2 .d.ts
files (with similar content)
api/token.d.ts
interface token { code: string; } export default token;
and index.d.ts
export * './api/token';
running grunt-dts-bundle following options
dts_bundle: { release: { options: { name: 'my-module', main: 'index.d.ts' } } }
will generate ambient module declaration file my-module.d.ts
following content
declare module 'my-module' { export * './api/token'; }
however declaration not compile due : import or export declaration in ambient module declaration cannot reference module through relative module name.
how can automatically generate ambient module declaration 2 typescript files above ?
edit
please follow latest updates on https://github.com/microsoft/typescript/issues/2262
i've written blog post this. summarize, can use autodts if replace index.ts
api.ts
, containing following:
export {default token} './api/token';
make sure api.ts
in same place api
directory (next to, not inside it).
then need package.json
file:
{ "name": "api", "version": "1.0.0", "main": "dist/api.js", "scripts": { "preinstall": "npm install autodts", "postinstall": "autodts link", "prepublish": "tsc && autodts generate" }, "typescript": { "definition": "index.d.ts" }, "dependencies": { "autodts": "~0.0.4" }, "devdependencies": { "@lib/autodts-generator": "~0.0.1", "typescript": "~1.5.3" } }
it's important package name api
matches file api.ts
, directory api
. way both node.js , typescript compiler in same places when using package.
finally, need tsconfig.json
file:
{ "compileroptions": { "declaration": true, "module": "commonjs", "target": "es5", "outdir": "dist" }, "files": [ "api.ts" ] }
now npm install
compile package , produce bundled index.d.ts
file defined in definition
setting in package.json
.
to use package, can like:
/// <reference path = "api/index.d.ts" /> import {token} 'api'; class foo { key: token; }
you can use autodts link
keep reference path
date, check blog post and/or autodts docs that.
the resulting index.d.ts
contains:
/// <reference path="index.ref.d.ts" /> declare module 'api/token' { interface token { code: string; } export default token; } declare module 'api' { export { default token } 'api/token'; }
index.ref.d.ts
, api.js
blank.
Comments
Post a Comment