【发布时间】:2022-01-12 07:03:17
【问题描述】:
我在 af 模块中有这个函数声明:
declare module 'picoapp' {
export function component(node?: HTMLElement): void
}
然后在 .ts 文件中像这样使用它
export default component((node: HTMLElement) => {
// All sorts of TS/JS here
})
但是 VSCode 给了我这个警告:Argument of type '(node: HTMLElement) => void' is not assignable to parameter of type 'HTMLElement'。
那么函数的返回类型应该是什么? 我没有返回值,只是使用节点作为参考。
【问题讨论】:
-
与返回类型无关。问题是您的类型声明说
component需要一个HTMLElement,而您却给了它一个函数。component应该期待回调吗?如果是这样,您必须修改它的类型。现在的回调应该只是传入的 HTML 元素吗?你必须修改代码。 -
...哇,我一定在睡觉...谢谢^^ 如何编写引用回调,接受 HTMLElement 作为参数?
-
(node: HTMLElement) => void假设回调不需要返回任何内容。如果是,那么void是预期的返回类型。例如,如果您希望它采用 HTMLElement 并返回true或false,则为(node: HTMLElement) => boolean
标签: typescript typescript-typings typescript-module-resolution