Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode provides intellisense for Foo.js via a corresponding Foo.d.ts only when it’s imported somewhere; how to enable intellisense in a Foo.js itself?

The setup is a “Create React App” with the following jsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "baseUrl": "src"
  },
  "include": ["src"]
}

The directory structure:

.
└── src
    └── Components
        └── Foo
            ├── Bar
            │   ├── Bar.js
            │   ├── Bar.d.ts
            │   └── index.js
            ├── Foo.js
            └── index.js

// React component `Foo` imports a component `Bar`:

import { Bar } from './Bar'

export function Foo(props) {
  //
}

// And gets full intellisense via `Bar.d.ts`:

type Props = {
  /** ... */
}

export declare function Bar(
  props: Props
): React.FunctionComponent

But Bar.js itself doesn’t get intellisense from its own Bar.d.ts file, is there a way to fix it? I tried the triple-slash directive (/// <reference path="Bar.d.ts"/>), but it didn’t help. Some JSDoc helped, but it’s rather pointless to have a dedicated declaration file and still use JSDoc; it also probably only works with VSCode, which is undesirable:

export function Bar(
  // When `type Props` is exported from the `.d.ts`:
  /** @type {import("./Bar").Props} */ props
) {
like image 239
awgv Avatar asked Aug 03 '20 19:08

awgv


People also ask

How do I get JavaScript IntelliSense in Visual Studio?

You can access the IntelliSense page by choosing Tools > Options on the menu bar, and then expanding Text Editor > JavaScript/TypeScript > IntelliSense. Your computer might show different names or locations for some of the Visual Studio user interface elements in this article.

What is JavaScript IntelliSense?

IntelliSense# Visual Studio Code's JavaScript IntelliSense provides intelligent code completion, parameter info, references search, and many other advanced language features. Our JavaScript IntelliSense is powered by the JavaScript language service developed by the TypeScript team.


1 Answers

As of September 2020, this is intended (source):

[...] there is no supported scenario for "the .d.ts is a sidecar to the .js". The recommended thing to do here is use JS Doc to write your type annotations.

Follow TypeScript’s development to see if it changes in the future.

like image 191
awgv Avatar answered Nov 15 '22 07:11

awgv