1
votes

I have a hard time figuring out how to solve the following issue because I don't know what to search for. Let me explain: I've written a class using TypeScript that I'm exporting:

class MyAPIClass {
  myMethod(one:number) : void;
  secondMethod(text:string) : number;
}
export = MyAPIClass;

Now I'm using that class in another TypeScript project of mine:

import MyAPIClass = require('../path/MyAPIClass');

let myClass = new MyAPIClass();
myClass.myMethod(1);

This is working fine, but I don't get the "typings". My editor doesn't recognize the types from the other file. I also can't do this:

let myClass : MyAPIClass = new MyAPIClass();

How can I "import" the typings too?

1
you should use import instead of require (I mean the keyword) - Mohammad Kermani

1 Answers

0
votes

Try instead

export class MyAPIClass {
  myMethod(one:number) : void;
  secondMethod(text:string) : number;
}

This exports the class as a named export. Then in your other file you can do

import {MyAPIClass} from '../path/MyAPIClass'

to bring it into your projects. From there

let myClass : MyAPIClass = new MyAPIClass();

should work just fine.