VSCode 用于绑定 document.createElement 参考的 Intellisense
VSCode Intellisense for bound document.createElement reference
我正在为 document.createElement
添加别名,以用作我的代码库中的快捷方式。 Visual Studio 代码似乎无法根据这个简单的声明推断出变量的类型,但是我不确定如何给它正确的提示。
我尝试添加一个@type 提示,例如@type {document.createElement}
或@type {Function}
。我认为如果我将它包装在我自己的函数中并通过 Element 或 HTMLElement 声明 return 类型,它会部分起作用。但这不会给我与 document.createElement
通常得到的相同的函数签名。
/** Shortcut for document.createElement - must have document bound to it */
const _ = document.createElement.bind(document);
我希望当我在下划线后键入括号时,我会看到与键入 document.createElement
.
相同的函数签名和参数提示
尝试将 typeof
与 @type
jsdoc 注释结合使用:
/** @type {typeof document.createElement} */
const _ = document.createElement.bind(document);
除了 @type
注释采用类型而 document.createElement
是运行时表达式外,您走在正确的轨道上。 TypeScript 中的 typeof
type query 接受一个表达式(例如 document.createElement
)和 returns 它的类型。
或者,您可以改用 Document
类型。全局 document
变量是 Document
类型的实例:
/** @type {Document['createElement']} */
const _ = document.createElement.bind(document);
我正在为 document.createElement
添加别名,以用作我的代码库中的快捷方式。 Visual Studio 代码似乎无法根据这个简单的声明推断出变量的类型,但是我不确定如何给它正确的提示。
我尝试添加一个@type 提示,例如@type {document.createElement}
或@type {Function}
。我认为如果我将它包装在我自己的函数中并通过 Element 或 HTMLElement 声明 return 类型,它会部分起作用。但这不会给我与 document.createElement
通常得到的相同的函数签名。
/** Shortcut for document.createElement - must have document bound to it */
const _ = document.createElement.bind(document);
我希望当我在下划线后键入括号时,我会看到与键入 document.createElement
.
尝试将 typeof
与 @type
jsdoc 注释结合使用:
/** @type {typeof document.createElement} */
const _ = document.createElement.bind(document);
除了 @type
注释采用类型而 document.createElement
是运行时表达式外,您走在正确的轨道上。 TypeScript 中的 typeof
type query 接受一个表达式(例如 document.createElement
)和 returns 它的类型。
或者,您可以改用 Document
类型。全局 document
变量是 Document
类型的实例:
/** @type {Document['createElement']} */
const _ = document.createElement.bind(document);