jvitor83 jvitor83 - 6 months ago 287
Node.js Question

Generated definition file (.d.ts) by typescript not working with package.json typings

I have created a definition file (d.ts) from my typescript project using the --declaration argument from the tsc compiler.

But this generated definition file apparently not work when i try to publish the package with the property typings at the npm package.json. I have created another project to test it.

It complains with the message: "Exported external package file typings file '...d.ts' is not a module. Please contact the package author to update the package definition".

This are my source files:

MyInterface.ts

export interface MyInterface
{
MyProperty: string;
}


MyClass.ts

import {MyInterface} from './MyInterface';

export class MyClass implements MyInterface
{
get MyProperty(): string
{
return "MyString";
}
}


MyInheritedClass.ts

import {MyClass} from './MyClass';

export class MyInheritedClass extends MyClass
{
public MyMethod(): number
{
return 1;
}
}


The tsc command is this:

tsc MyClass.ts MyInterface.ts MyInheritedClass.ts --declaration -t es5 -outFile "mydefinition.js" --module "system"


This is the generated definition:

mydefinition.d.ts

declare module "MyInterface" {
export interface MyInterface {
MyProperty: string;
}
}
declare module "MyClass" {
import { MyInterface } from "MyInterface";
export class MyClass implements MyInterface {
MyProperty: string;
}
}
declare module "MyInheritedClass" {
import { MyClass } from "MyClass";
export class MyInheritedClass extends MyClass {
MyMethod(): number;
}
}


Is there another thing i have to do or the generated definition supposedly not work in the package.json typings ??

Update for more details:

The scenario is that i have 2 projects:


  • The First project is where i generate the library where i will publish to npm, and the package.json is where i put the typings property linking to the mydefinition.d.ts file generated by the tsc -d command.

  • The Second project i create to test the first one by adding the package (with typings) generated.



A link with the 2 projects and another links for the scenario :

https://github.com/jvitor83/typings-packagejson

I think the problem is with the "Your definition files should be written as external modules" at the first link.
But i want to know if there is a way to get those definitions generated to put at the typings property of the package.json.

To reproduce the problem:


Answer

The solution was:

1 - add a single file 'index.ts' that exports all others files.

export * from "./MyInterface"
export * from "./MyClass"
export * from "./MyInheritedClass"

2 - add at build process two compilation/transpilation of the files (one for declarations and another for the single js file [with out property in tsc compiler])

let tsConfigDeclaration = gulp_typescript({module: 'system', target: 'es5', declaration: true, removeComments: true });
let tsConfigOneFile = gulp_typescript({module: 'system', target: 'es5', declaration: true, removeComments: true, out: 'typings-packagejson.js'});

More info at build process: https://github.com/jvitor83/typings-packagejson/blob/master/gulpfile.js

and index file: https://github.com/jvitor83/typings-packagejson/blob/master/app/src/typings-packagejson.ts

Comments