VSCode 仅在导入到某处时才通过相应的 Foo.d.ts 为 Foo.js 提供智能感知;如何在 Foo.js 本身中启用智能感知?

Ban*_*rew 7 javascript typescript reactjs visual-studio-code

设置是一个“创建 React 应用程序”,具有以下内容jsconfig.json

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "baseUrl": "src"
  },
  "include": ["src"]
}
Run Code Online (Sandbox Code Playgroud)

目录结构:

.
??? src
    ??? Components
        ??? Foo
            ??? Bar
            ?   ??? Bar.js
            ?   ??? Bar.d.ts
            ?   ??? index.js
            ??? Foo.js
            ??? index.js
Run Code Online (Sandbox Code Playgroud)
// React component `Foo` imports a component `Bar`:

import { Bar } from './Bar'

export function Foo(props) {
  //
}
Run Code Online (Sandbox Code Playgroud)
// And gets full intellisense via `Bar.d.ts`:

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

export declare function Bar(
  props: Props
): React.FunctionComponent
Run Code Online (Sandbox Code Playgroud)

但是Bar.js它本身并没有从它自己的Bar.d.ts文件中获得智能感知,有没有办法解决它?我尝试了三斜线指令 ( /// <reference path="Bar.d.ts"/>),但没有帮助。一些 JSDoc 有所帮助,但拥有专用声明文件并仍然使用 JSDoc 是毫无意义的;它也可能只适用于 VSCode,这是不可取的:

export function Bar(
  // When `type Props` is exported from the `.d.ts`:
  /** @type {import("./Bar").Props} */ props
) {
Run Code Online (Sandbox Code Playgroud)

Ban*_*rew 2

截至 2020 年 9 月,预计将实现以下目标(来源):

\n
\n

[...] 不支持“.d.ts 是 .js 的边车”的场景。这里推荐使用 JS Doc 来编写类型注释。

\n
\n

关注TypeScript\xe2\x80\x99s的开发,看看未来是否会发生变化。

\n