我正在定义以下Typescript接口。clickCustomButton1应该什么也不返回,但是我不确定如何指定。
interface IButtonTemplate {
clickCustomButton1: (); // How can I say this should return nothing?
// more code here
}
我在代码中使用以下代码:
clickCustomButton1: null
然后再:
newTopicTests = () => {
}
clickCustomButton1 = this.newTopicTests();
它给我一个错误,说:
Error 2 Cannot convert 'void' to '() => boolean'
有人可以告诉我我在做什么错吗?我想我需要做的是指定clickCustomButton1和newTopicTests不返回任何内容。但是如何使用Typescript做到这一点?
问题是因为lambda() => {}
的键入方式是(): void
因为它不返回任何内容,因此没有推断出的[other]类型。
因此,如果给定f = () => {}
,则表达式f()
也将键入为void
-,但是clickCustomButton1必须返回boolean
声明的a。
比较使用以下lambda(类型为(): boolean
,现在为类型有效)的情况:
newTopicTests = () => true
解决此问题的另一种方法是将原始代码编写为:
newTopicTests = (): boolean => {}
(这也将无法编译,但是会在错误的源头附近显示该错误。)
在更新问题之后。
要在接口中声明一个方法不返回任何内容,请使用:
clickCustomButton1(): void;
要声明类型为的成员(): void
,请使用
clickCustomButton1: () => void;
还请注意,这null
是某种事物,而虚空则什么也不代表。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句