6

I have a Typescript/JavaScript hybrid project and I'm creating declaration d.ts files for some of the JavaScript files in order to aid in the management of multiple classes and subclasses.

However the declared types are not being seen by Visual Studio Code. I have the js and corresponding d.ts files in the same directory.

For example:

my-class.js

class MyClass {
  constructor(options) {}

  action(num) {
    return num.toString()
  }
}

}

my-class.d.ts

export interface Options {
  a: boolean
  b: boolean
}

export declare abstract class MyClass {
  constructor(options: Options)
  action(num: number): string
}

IntelliSense does not pick up the definitions, for instance, when I hover over the action method of my-class.js.

enter image description here

I've tried adding a reference annotation at the top of my-class.js with no effect (///<reference path="./my-class.d.ts" />).

I have the checkJs option set to true in my settings.

I'm using VS Code Version 1.19.1

Are there other actions I need to take in order for this to work?

2
  • Try opening folder, not files. I think I found in the past (been several revisions of Code since I have done any .ts edits) that Code had issues doing cross file intellisense if you opened files individually. Commented Jan 4, 2018 at 16:49
  • Added VS Code version to the question. Commented Jan 4, 2018 at 17:26

1 Answer 1

4

Make sure you have a jsconfig.json file in your working folder. After you have this config file your .d.ts files should be included/reference.

enter image description here

Regarding your specific example, as far as I know, it's not going to work as you expect. This is because of declaration scope. Meaning, in your .d.ts file you define a MyClass with TypeScript types, then in your .js file you define a second MyClass, this second declaration also has types, it has default types of any. If you were inheriting from the first class then it may work as your expecting.

That being said there are things that will work:

  • You should get IntelliSense on the Instance of the class
  • You can add JSDoc comments to your .js files to define/link the .d.ts types

Example:

my-class.d.ts

declare interface Options {
    a: boolean
    b: boolean
}

declare class MyClass {
  constructor(options: Options)
  action(num: number): string
}

my-class.js

class MyClass {
  /**
  * @param {Options} options 
  */
  constructor(options) {   
  }

  action(num) {
    return num.toString();
  }
}
var foo = new MyClass({ a: true, b: false } );
foo.action(88);

If you hover over the foo instance in the .js file you will notice that you do get correct IntelliSense

enter image description here

By adding the JSDoc Comment "@param {Options} options" to the constructor() you will also get correct IntelliSense

enter image description here

JSDoc and TypeScript: https://github.com/Microsoft/TypeScript/wiki/JsDoc-support-in-JavaScript VSCode and TypeChecking: https://code.visualstudio.com/docs/languages/javascript#_global-variables-and-type-checking

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

3 Comments

Justin, thanks for the response. I do have a jsconfig.json in the working root. I'll try what you suggest when I'm able (tomorrow); however I was trying to avoid using JSDoc annotations everywhere, hence the .d.ts files. Wrt scope, I was under the impression that the .d.ts declared the classes, interfaces, etc., while the .js files defined them. Thanks again. I'll give this a shot.
@t.888 did you manage to get it to work (get intellisense in the .js file for which the .d.ts was defined)?
@EECOLOR I have not. I use JSDoc to define types, which works inside the .js module file, and of course the .d.ts file works for any consumers of the .js module, but not inside the module itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.