0

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.

3
  • constructor(name) That's the same as doing constructor(name:any) So any <> string.. Commented Jul 2, 2018 at 15:22
  • There are no constructors for types only for classes Commented Jul 2, 2018 at 15:23
  • What are you trying to describe with the tbrat type? Right now it reads "a value of type tbrat has a constructor property which is a function that takes a string and produces a tbrat." That doesn't match a Parser object for a few reasons; one is that a newable function isn't the same as a regular function, and the other is that the constructor property 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. Commented Jul 2, 2018 at 15:31

2 Answers 2

4

The same way that JavaScript has modules, TypeScript does, too.

If you were writing pure TypeScript, you would not need to do the following, as the transpiler would take care of this for you. However, as you are creating a definition file, you need to declare a module in the same way that your JavaScript code does.

You need to declare your parser module in parser.d.ts:

declare module "parser" {
  class Parser {
    constructor(name:string): void
  }

  export default Parser;
}

You then import parser as you normally would in utilize.ts:

import Parser from 'parser'

const foo = new Parser('test')

Further reading: https://www.typescriptlang.org/docs/handbook/modules.html

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks apsillers, hopefully it is helpful. Will keep this in mind for future answers!
Ive tryed to keep my JS as I need and modify the parser.d.ts but it is not allowed to use void in enviroment context. So it fails. And also it fails in the usage because semms I can't use 'new' without a constructor signature.
codepen.io/nowy5/project/editor/DBjPVx Hope this helps
Thanks but It gives me this error "Cannot use 'new' with an expression whose type lacks a call or construct signature."
Get It. when i use it i have to call it in this way: const foo = new Parser.Parser('test'). Thanks for the help
|
1

There are no constructors for type definitions only for classes, so you might want to use declare, something like the following will work:

class Parser {
  constructor(name){
    name;
  }
} 

declare class tbrat {
    constructor(name: string) {

    }
}

const n: tbrat = new Parser('hello');

Please also take a look at the answer from Noel Varanda.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.