【问题标题】:VSCode Intellisense for bound document.createElement reference用于绑定 document.createElement 参考的 VSCode Intellisense
【发布时间】:2019-08-27 14:56:41
【问题描述】:

我在我的代码库中使用别名 document.createElement 作为快捷方式。 Visual Studio Code 似乎无法根据这个简单的声明推断变量的类型,但我不确定如何给出正确的提示。

我尝试添加@type 提示,例如@type {document.createElement}@type {Function}。我认为如果我将它包装在我自己的函数中并通过 Element 或 HTMLElement 声明返回类型,它会部分工作。但这不会给我与document.createElement 通常得到的相同的函数签名。

/** Shortcut for document.createElement - must have document bound to it */
const _ = document.createElement.bind(document);

我希望当我在下划线后面输入括号时,我会看到与输入 document.createElement 相同的函数签名和参数提示。

【问题讨论】:

    标签: javascript ecmascript-6 visual-studio-code intellisense


    【解决方案1】:

    尝试使用typeof 结合@type jsdoc 注释:

    /** @type {typeof document.createElement} */
    const _ = document.createElement.bind(document);
    

    除了@type 注释采用类型而document.createElement 是运行时表达式之外,您的做法是正确的。 TypeScript 中的typeof type query 接受一个表达式(例如document.createElement)并返回其类型。

    或者,您可以改用Document 类型。全局document 变量是Document 类型的一个实例:

    /** @type {Document['createElement']} */
    const _ = document.createElement.bind(document);
    

    【讨论】:

    • 啊!太棒了,非常感谢!我总是对在哪里查找这些信息感到困惑(JSDoc 文档与 TypeScript 文档)。
    猜你喜欢
    • 2020-08-25
    • 2021-07-26
    • 2022-01-08
    • 2020-10-09
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多