javascript – How do I use TypeScript typing information from a Node.js package?

I’ve read a ton of different variations of this same question, but I just can’t wrap my mind around it. I’m using the websocket module (which I’ve used many times before without TypeScript) and I can’t figure out how to type my variables?

I have a function that takes in a WebSocketConnection object, but when I give it the type WebSocketConnection I get a "Cannot find name 'WebSocketConnection'" from TypeScript.

I’ve installed the @types/websocket package and I can see the index.d.ts file with all the type definitions in it located in ./node_modules/@types/websocket/index.d.ts relative to my tsconfig.json file…

I’ve tried adding the "typeRoots" option to the tsconfig.json file, as well as "types". I’ve tried many combination of values but as far as I can tell leaving them off entirely is a better bet, so I’ve tried that as well. I’ve also tried many variations of importing data from both the .d.ts file and also the package itself, with no luck of course.

I tried using /// <reference path="node_modules/@types/websocket/index.d.ts" /> also with no luck.

I looked in the .d.ts file and found a very clear declaration of an interface called IStringified that looked like this:

export interface IStringified {
    toString: (...args: any[]) => string;

So I tried to access IStringified and I’m still getting the same "Cannot find name 'IStringified'" error.

I’m probably just being really dumb and missing something plainly obvious, but any pointers or advice would be much appreciated! What in the world am I doing wrong?

Read more here: Source link