gpt4 book ai didi

javascript - javascript 文件中的函数无法使用声明文件检测自己的类型

转载 作者:行者123 更新时间:2023-12-04 11:18:44 27 4
gpt4 key购买 nike

有相应声明文件的文件是否可以在 vscode 中检测到自己的类型?
其他文件可以获取类型,但不能获取文件本身。
注意任何
enter image description here
但这有正确的打字
enter image description here
再生产

  • 创建文件夹
  • 运行npm init
  • 点击进入所有内容
  • 创建3个文件index.js index.d.ts import.js
  • index.js
    async function test(input) {
    return null
    }
    index.d.ts
    type input_type = {
    name: string,
    salary: number
    }

    export function test(input : input_type) : Promise<null>
    import.js
    import * as t from './index'

    t.test()
    当您将鼠标悬停在 test 上时 index.js中的函数它不知道它的参数类型。但是在 import.js它知道。
    我怎样才能得到 index.js test函数知道自己的类型。

    最佳答案

    据我了解,VSCode 无法推断类型,因为该类型不存在于 index.js 的范围内。但是,它被导入到 import.js 中。
    我能想到的唯一解决方法是帮助 VSCode 使用 JSDoc 推断类型,即使这样我们也必须导入类型:

    /**
    * @typedef {import(".").input_type} input_type
    * @param {input_type} input
    */
    async function test(input) {
    return null
    }
    显示工作:
    Solution shown working

    关于javascript - javascript 文件中的函数无法使用声明文件检测自己的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67155361/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com