I'm new in Typescript and I'm trying to migrate a JS customized library to TS by adding all types. This is an easy example of what I am try to do. Original JS File (Class) 'parser.js':
class Parser {
constructor(name){
this.name = name;
}
}
module.exports = Parser;
Types file 'parsertypes.d.ts':
export type tbrat = {
constructor(name:string): tbrat;
};
TS utilizing file 'utilize.ts':
import Parser from './parser';
import {tbrat} from './parsertypes';
const n: tbrat = new Parser('hello');
ERROR:
Type 'Parser' is not assignable to type 'tbrat'. Types of property 'constructor' are incompatible. Type 'Function' is not assignable to type '(name: string) => tbrat'. Type 'Function' provides no match for the signature '(name: string): tbrat'.
I don't understand what I am missing. I can't move the original JS file to TS for particular reasons.
constructor(name)That's the same as doingconstructor(name:any)Soany<>string..tbrattype? Right now it reads "a value of typetbrathas aconstructorproperty which is a function that takes astringand produces atbrat." That doesn't match aParserobject for a few reasons; one is that a newable function isn't the same as a regular function, and the other is that theconstructorproperty of a class instance isn't very strongly typed in TypeScript. You could fix both of these issues, but I don't understand why you need this.