How do I use a class defined in a TypeScript declaration file?

Joe Martella

I'm attempting to create a typings file for this simple queue implementation.

In my TS project, I've created a folder called customTypings and pointed to it in the typeRoots property of my tsconfig.json file.

Here's what my .d.ts file looks like:

declare module 'queue-fifo' {
    export default class Queue {
        constructor();
        isEmpty(): boolean;
        size(): number;
        clear(): void;
        enqueue(data: any): void;
        dequeue(): any;
        peek(): any;
    }
}

I import is as: import Queue from 'queue-fifo';

And then I try to create an instance of the Queue class: const queue = new Queue();

At this point, I get no type errors in VS Code, nor do I get any compilation errors. However, when I try to run my code through the debugger, I get:

Exception has occurred: TypeError
TypeError: queue_fifo_1.default is not a constructor
    at BinarySearchTree.bfs (/../binarySearchTree.ts:110:23)
    at testBst (/../binarySearchTree.ts:139:10)
    at Object.<anonymous> (/../binarySearchTree.ts:144:1)
    at Module._compile (module.js:632:14)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)

If I break at that line, I see the Queue (what I imported) is undefined, but queue_fifo_1 is defined, and I can create an instance of the class using that name while in the debug console.

Can someone explain what I'm doing wrong in my declaration/consumption of the declaration that is causing this undesired behavior?

Aaron Beall

Solution

The queue-fifo module uses CommonJS-style export = Queue, which is not compatible with ES6 imports, and gives you no default export. The correct type definition will need to use the same export = style syntax:

declare module "queue-fifo" {
    class Queue {
        isEmpty(): boolean;
        size(): number;
        // etc
    }
    export = Queue;
}

Which can be imported using CommonJS-style import:

import Queue = require("queue-fifo");
const queue = new Queue();

ES6 import

If you're wondering if it's possible to import this module using ES6 syntax, you can probably use the namespace definition hack:

class Queue { /* ... */ }
namespace Queue { /* empty */ }
export = Queue;

Then import it using ES6 wildcard:

import * as Queue from "queue-fifo";
const queue = new Queue();

But this will only work in non-ES6 environments, for example bundlers like Webpack and Rollup make this work today, but future ES6 compliant module systems will not be able to make this work. See more here: What does "... resolves to a non-module entity and cannot be imported using this construct" mean?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I use a custom Typescript Declaration file (.d.ts) from within Angular?

From Dev

How do I write a proper class definition & declaration when a member is a user defined class?

From Dev

How do I write a TypeScript declaration file for a complex external commonjs module that has constructor, such as imap?

From Dev

How do I extend a TypeScript class definition in a separate definition file?

From Dev

How do I point a TypeScript property to a class object in a definition file?

From Dev

How do I use an class defined array and add values (pre-defined parameters) to it?

From Dev

How to define a singleton javascript class in a typescript declaration file

From Dev

How do I use forward class declaration for circular dependencies in an iOS iPhone app?

From Dev

How to use defined in class file with namespace

From Dev

How do I use "then" with TypeScript?

From Dev

In Typescript, how can I use functions defined in another TS file without puting them in a module?

From Dev

How do I use java Scanner class with complete file path?

From Dev

How do I use a method defined in another class, without a calling object?

From Dev

How do I modify class declaration syntax correctly with Roslyn?

From Dev

How do I correctly use enable_if in this specific function declaration?

From Dev

How do these two functions defined in the same class manage to call each other without forward declaration?

From Dev

How do i change a variable that i have defined in a different class

From Dev

How do I use Sinon with Typescript?

From Dev

How do I use pouchdb with typescript?

From Dev

How do I add an Object to a enum defined class?

From Dev

How do I create and recall class objects from a resource file that I can use at runtime?

From Dev

In Emberjs, how do you use in the template file, properties defined in the route?

From Dev

How to use TypeScript ambient declaration interfaces in interface declared in d.ts file

From Dev

how to use a defined class in ckeditor?

From Dev

How do I use a Java-defined instance method in Lua?

From Dev

How do I use C++ vectors with a user defined structure?

From Dev

How do I use a User Defined Type in a function?

From Dev

How do I use setTimeout with recursion to wait for a variable to be defined in JavaScript?

From Dev

How do I use an ITK class in Python

Related Related

  1. 1

    How do I use a custom Typescript Declaration file (.d.ts) from within Angular?

  2. 2

    How do I write a proper class definition & declaration when a member is a user defined class?

  3. 3

    How do I write a TypeScript declaration file for a complex external commonjs module that has constructor, such as imap?

  4. 4

    How do I extend a TypeScript class definition in a separate definition file?

  5. 5

    How do I point a TypeScript property to a class object in a definition file?

  6. 6

    How do I use an class defined array and add values (pre-defined parameters) to it?

  7. 7

    How to define a singleton javascript class in a typescript declaration file

  8. 8

    How do I use forward class declaration for circular dependencies in an iOS iPhone app?

  9. 9

    How to use defined in class file with namespace

  10. 10

    How do I use "then" with TypeScript?

  11. 11

    In Typescript, how can I use functions defined in another TS file without puting them in a module?

  12. 12

    How do I use java Scanner class with complete file path?

  13. 13

    How do I use a method defined in another class, without a calling object?

  14. 14

    How do I modify class declaration syntax correctly with Roslyn?

  15. 15

    How do I correctly use enable_if in this specific function declaration?

  16. 16

    How do these two functions defined in the same class manage to call each other without forward declaration?

  17. 17

    How do i change a variable that i have defined in a different class

  18. 18

    How do I use Sinon with Typescript?

  19. 19

    How do I use pouchdb with typescript?

  20. 20

    How do I add an Object to a enum defined class?

  21. 21

    How do I create and recall class objects from a resource file that I can use at runtime?

  22. 22

    In Emberjs, how do you use in the template file, properties defined in the route?

  23. 23

    How to use TypeScript ambient declaration interfaces in interface declared in d.ts file

  24. 24

    how to use a defined class in ckeditor?

  25. 25

    How do I use a Java-defined instance method in Lua?

  26. 26

    How do I use C++ vectors with a user defined structure?

  27. 27

    How do I use a User Defined Type in a function?

  28. 28

    How do I use setTimeout with recursion to wait for a variable to be defined in JavaScript?

  29. 29

    How do I use an ITK class in Python

HotTag

Archive