typescript - 为什么 Typescript 让我在一个应该返回 void 的函数中返回一个非 void 值
问题描述
我有这个类声明:
class Event {
action: () => void;
constructor(identifier: string, action: () => void) {
(...)
}
}
然后,Typescript 让我从该类构造一个带有action
参数函数的对象,该函数不会返回 void 而不会出错,例如:
const newObject = new Event("string", () => {
return "string";
});
但是,如果我将参数函数的返回类型更改action
为string
例如在类声明中,然后构造一个其参数action
返回数字的对象,它确实会显示预期的错误:
Argument of type '() => number' is not assignable to parameter of type '() => string'.
Type 'number' is not assignable to type 'string'.
解决方案
推荐阅读
- sql - 尝试连接到 SQL Server 时出错
- java - 为什么 postValue() 不能在新线程中工作?
- ansible - 如何将变量文件从集合中包含到 ansible 剧本中
- c - 使用 dsPIC33E 的 FFT 不返回频率
- autodesk-forge - Forge 查看器:setNavigationLock 在 BimWalk 模式下无法正常工作
- php - Apache 服务器不加载扩展(Windows 10、PHP 8.0.12、Apache 2.4.46、(WAMP3.2.3))
- c# - 如何重新排列列表索引
- arules - 在新数据上使用来自规则的关联
- pandas - 是否有一种简单的方法可以在熊猫框架中跨行选择以创建新列?
- flutter - 如何为位置因布局更改而更改的 Flutter 小部件设置动画