iwhp iwhp - 2 months ago 24
TypeScript Question

Generating a d.ts file with the compiler option generates a error for angular

When using the tsc compiler option

"declaration": true
I get the following error:

error TS4019: Implements clause of exported class 'MyClass' has or is using private name 'angular'.


I do not get the error when using
"declaration": false
.

Using TypeScript Version: 2.0.3

Sample Code:

MyClass.ts

/// <reference path="node_modules/@types/angular/index.d.ts" />
module MyModule {
export class MyClass implements angular.IServiceProvider {
constructor() {
}
$get;
}
}


tsconfig.json

{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5",
"declaration": true
},
"exclude": [
"node_modules"
]
}


There is a github repo on https://github.com/iwhp/issues/tree/master/typescript-bug-declaration-file-with-angular

Expected behavior:

A .d.ts file should be created.

Addition to the sample above

Changes to MyClass.ts

import * as angular from 'angular';
export module MyModule {
export class MyClass implements angular.IServiceProvider {
constructor() {
}
$get;
}
}


MyClass2.ts

export module MyModule {
export class MyClassB implements angular.IServiceProvider {
constructor(myClass: MyModule.MyClass) {
}
$get;
}
}


Getting the following Compiler error:

error TS2305: Module '"MyClassB".MyModule' has no exported member 'MyClass'.

Answer

If you are always going to compile it with typescript 2.0, you can replace this line

/// <reference path="node_modules/@types/angular/index.d.ts" />

with more modern

import * as angular from 'angular';

Then .d.ts file is created, but it's empty because you did not export MyModule - you need an export for it too:

export module MyModule {