typescript - 类型保护没有像预期的那样缩小泛型类型。“T”可以用与“数字”无关的任意类型实例化
问题描述
我有一个插入值的函数,用于为对象设置动画。值可以是数字、数字数组或包含数字/数字数组的对象,尽管这对于即将描述的问题并不重要。
这是源代码的骨架,可在此TS 游乐场链接中找到:
function lerpGeneric<T>(from: T, to: T, progress: number): T {
if (typeof from !== typeof to) {
throw Error(`The types of 'from' and 'to' do not match. Got ${typeof from} and ${typeof to}`);
}
switch (typeof from) {
case 'number':
if (typeof to === 'number')
return lerpNumber(from, to, progress); // Error! 'T' could be instantiated with an arbitrary type which could be unrelated to 'number'.
else
throw Error(`Expected type of 'to' to be number but got ${typeof to}.`);
case 'object':
// If array, clone and iterate over each item.
// If object, clone and recurse over properties.
default:
// not lerp-able, throw error.
}
}
// Linearly interpolates a value. Progress is a number from 0 to 1.
function lerpNumber(start: number, end: number, progress: number): number {
return start + (end - start) * progress;
}
在标有 的行 // Error!
。编译器抱怨返回的类型。switch
对我来说, and的类型检查if
应该(有效地)缩小T
to的类型number
,但这不会发生。编译器说'T' could be instantiated with an arbitrary type which could be unrelated to 'number'.
有趣的是,VSCode 建议了from
和to
to be的类型T & number
,而不仅仅是number
,就像我期望的那样。无论如何,编译器警告是什么?我想不出任何T
返回typeof T === 'number'
anumber
可能导致运行时错误的类型。有没有办法在这里更正处理类型,还是我只需要在这里执行类型断言?
解决方案
TypeScript 错误地推断出的类型from
是T & number
when,事实上,它只能number
在那个时候。它看起来像一个 TypeScript 错误,你肯定不是第一个注意到它的人。
在修复此 TypeScript 错误之前,请if
使用switch
.
推荐阅读
- angular - 观察者函数中的异步等待
- gitlab - GitLab CI CD - 从外部项目获取变量
- python - django.db.utils.ProgrammingError: 列calculator_calculation._id 不存在使用heroku 和djongo
- ios - 白色或浅色 ASAuthorizationAppleIDButton “使用 Apple 登录”按钮 - Swift
- reactjs - 将 React Route 传递给子组件
- javascript - 如何在 javascript 中将日期与 01-Nov-2019 等格式匹配
- snowflake-cloud-data-platform - Snowflake:使用 UDF 复制到 SQL
- python - 仅保留 2 级多索引 Pandas 数据帧的一列值,同时忽略 NaN
- python - Tkinter:在画布内制作一个画布宽度的框架
- azure - Azure VM 是否需要托管磁盘?