Adam Moszczyński
Adam Moszczyński

Reputation: 3556

how to use auto generated typescript declaration files in separate project?

I have developed typescript library which consists of several classes located in separate ts files.

app.ts

//imported class from separate file    
import leafAdd = require('./Base/leaf');


export class MyClass {
    public leafInstance : leafAdd.Leaf;
}

var MyClassInstance = new MyClass();

The code works fine so i want to use my library in a new separate project, still I do not want to transfer whole code. Instead I just want to use definition files from typescript compiler (generated by adding --declarations flag). The declaration files look like this:

app.d.ts

import leafAdd = require('./Base/leaf');

export declare class MyClass {
    public leafInstance : leafAdd.Leaf;
}

When I try to reference this file in a new project it somehow does not work:

newapp.ts

/// <reference path="./typings/app.d.ts" />

export class MyOtherClass{
    public myClass: MyClass; //Compiler error: Could not find symbol MyClass
    constructor() {
        //some other code
    }
}

And I got compiler error saying: "Could not find symbol MyClass"

Perhaps I am missing something obvious or just trying to reference the code in a wrong way. I would be really gratefull if someone can point me to the right direction.

Upvotes: 1

Views: 660

Answers (1)

Adam Moszczyński
Adam Moszczyński

Reputation: 3556

Ok I got it. I should have looked at app.d.ts file for solution :). Simply, it seems that declaration files containing

export declare class ...

can not be referenced using

/// <reference path="./path/to/declaration.d.ts" />

and should be imported like any other module/class. So working code looks like this:

import myclMod = require('./typings/app') //please notice missing file extension - it confused me at the beginning :)
export class MyOtherClass{
    public myClass: myclMod.MyClass; 
    constructor() {
        //some other code
    }
}

Upvotes: 1

Related Questions