mode777 mode777 - 15 days ago 9
TypeScript Question

Typescript: Type of exported AMD class not found

Im using Typescript with a requirejs setup, so typescript is configured to use AMD modules.

I'm trying to export a class and use it from another one. Here is some example code.

//file: ClassA.ts
export = class ClassA {
}

//file: ClassB.ts
import ClassA = require("ClassA")

export = class ClassB {
constructor(classA: ClassA) { //ClassA is not found
// However this will work:
let c = new ClassA();
}
}


When compiling I get the following error.


ClassB.ts(4,25): error TS2304: Cannot find name 'ClassA'.


However intellisense is fine with the instanciation of ClassA and I get full intellisense for the instance.

So what am I doing wrong here?

EDIT: Here is my tsconfig.json

{
"compileOnSave": true,
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5",
"module": "amd"
},
"exclude": [
"node_modules",
"wwwroot"
]
}

Answer

It looks like you are mixing JavaScript and TypeScript syntax for exporting and importing classes.
Try this syntax:

ClassA.ts:
export class ClassA {
    print() {
        console.log(`ClassA.print()`);
    }
}

ClassB.ts:
import {ClassA} from './ClassA';

class ClassB {
    print() {
        let a = new ClassA();
        a.print();
    }
}