Porfírio
Porfírio

Reputation: 185

Generate definitions .d.ts with transpileModule

I am using TypeScript compiler API to generate js from ts.

My ts code is generated, so it's not saved in any file and i am using transpileModule to generate the js directly without needing to save a temporary ts file.

https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#transpiling-a-single-file

But i would like to have the option to generate declarations close to the js files. I am passing declaration and declarationDir to compiler options but it does nothing...

export const defaultCompilerOptions: ts.CompilerOptions = {
  target: ts.ScriptTarget.Latest,
  module: ts.ModuleKind.ES2015,
  lib: ['es2015'],
  strict: true,
  declaration: true,
  declarationDir: '/tmp/xpto',
};
const gen = ts.transpileModule(tsCode, { compilerOptions, fileName, reportDiagnostics: true });

Upvotes: 3

Views: 1452

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 220964

You can't produce declaration files using transpileModule because this function only performs syntactic transformations (whereas declaration file emitting requires semantic information). You'll need to use the usual createProgram function shown at the top of that page.

Upvotes: 2

Related Questions