javascript文件中的函数无法通过声明文件检测其自身的类型

A. *_*Lau 14 javascript typescript visual-studio-code typescript-typings

有对应声明文件的文件是否有可能在vscode中检测自己的类型?

其他文件可以获取类型,但不能获取文件本身。

注意任何

在此处输入图片说明

但这有正确的打字

在此处输入图片说明

再生产

  1. 创建文件夹
  2. npm init
  3. 点击进入通过一切
  4. 创建 3 个文件 index.js index.d.ts import.js

index.js

async function test(input) {
    return null
}
Run Code Online (Sandbox Code Playgroud)

index.d.ts

type input_type = {
    name: string,
    salary: number
}

export function test(input : input_type) : Promise<null>
Run Code Online (Sandbox Code Playgroud)

import.js

import * as t from './index'

t.test()
Run Code Online (Sandbox Code Playgroud)

当您将鼠标悬停在其中的test函数上时,index.js它不知道其参数类型。但在import.js它知道它。

我怎样才能让index.js test函数知道它自己的类型。

TUT*_*MON 9

据我了解,VSCode 将无法推断类型,因为该类型不存在于 index.js 的范围内。但是,它被导入到 import.js 中。

我能想到的唯一解决方法是使用 JSDoc 帮助 VSCode 推断类型,即使这样我们也必须导入类型:

/**
 * @typedef {import(".").input_type} input_type
 * @param {input_type} input 
 */
async function test(input) {
    return null
}
Run Code Online (Sandbox Code Playgroud)

显示工作:

解决方案显示有效