typescript - 如何使用未使用的接口
问题描述
我正在使用noUnusedLocals
打开的打字稿并且不想将其关闭。
但现在我正在尝试应用某种附加断言,如下所述:
interface ISmth<T> {
id: number;
data: T;
}
var a = [{
id: 1,
data: [],
}, {
id: 2,
data: 4,
}, {
id: 3,
data: "abc",
}] as const;
type ObjectIfImplemented = (typeof a extends Readonly<ISmth<any>[]> ? Object : never);
interface AssertThatReallyImplemented extends ObjectIfImplemented { }
所以我需要接口AssertThatReallyImplemented
作为编译断言(如果它编译,那么一切都很好,如果不是 - 我必须修复一些东西),但我不会在任何地方使用它。
有没有更好的方法来处理错误
'AssertThatReallyImplemented' 已声明但从未使用过。
然后以某种方式导出界面
export type _BypassNoUnusedLocalsLimitation = AssertThatReallyImplemented;
解决方案
给定noUnusedLocals
编译器选项和非导出类型断言接口,您可以使用@ts-ignore注释来抑制'AssertThatReallyImplemented ' is declared but its value is never read
错误。
为此,我们使用占位符 type 将上述“未声明”错误与实际类型断言错误分开,_
否则@ts-ignore
会使两者无效:
// @ts-ignore separate noUnusedLocals error from the type assertion error
type _ = AssertThatReallyImplemented
interface AssertThatReallyImplemented extends ObjectIfImplemented { }
出于测试目的,断言类型可以像这样通用:
type AssertAssignable<T, U extends T> = true
// @ts-ignore success case
type _ = Assert
type Assert = AssertAssignable<readonly ISmth<any>[], typeof a>
// @ts-ignore error case
type _ = AssertError
type AssertError = AssertAssignable<readonly ISmth<any>[], typeof aError> // error(OK!)
推荐阅读
- python - Python 停止执行当前缩进级别
- azure - 使用 OIDC 的 SSO 不适用于 Azure AD 加入设备的电子应用程序
- javascript - SVG 未显示在我的本地主机上 Apache v3.3.0
- r - R 中的 FAMD 出现重复的“row.names”错误?
- asp.net - 如何与物理打印机通信并在按钮单击时打印
- python - 获取有关在主机内工作的 RAM 的硬件信息但作为 Rundeck 作业失败的 Python 脚本
- python - 从 anaconda shell 运行 github 源代码时出错
- zebra-printers - Zebra 删除队列并标记/丢弃最后一个标签
- reactjs - React - 更改查询参数后防止滚动到顶部
- c - 其他方式强制进行另一轮 C 宏扩展?