VSCode仅在将Foo.js导入到某处时才通过相应的Foo.d.ts提供intellisense;如何在Foo.js本身中启用intellisense? [英] 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?

查看:49
本文介绍了VSCode仅在将Foo.js导入到某处时才通过相应的Foo.d.ts提供intellisense;如何在Foo.js本身中启用intellisense?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置是带有以下 jsconfig.json 的"Create React App":

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

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

目录结构:

.
└── 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

但是 Bar.js 本身不会从其自己的 Bar.d.ts 文件中获得智能感知,是否可以解决该问题?我尝试了三斜杠指令(///< reference path ="Bar.d.ts"//> ),但它没有帮助.一些JSDoc可以提供帮助,但是拥有专用的声明文件却仍然使用JSDoc毫无意义.它可能也只能与VSCode一起使用,这是不可取的:

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
) {

推荐答案

从2020年9月开始,这是有意的(

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

[...]没有".d.ts是.js的辅助工具"的受支持方案.推荐的做法是使用JS Doc编写类型注释.

[...] 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.

跟随TypeScript的发展,看看它将来是否会改变.

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

这篇关于VSCode仅在将Foo.js导入到某处时才通过相应的Foo.d.ts提供intellisense;如何在Foo.js本身中启用intellisense?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆