typescript - 部分擦除 TypeScript 中的类型参数
问题描述
我不完全确定解释我的问题的正确术语,所以这里有一个非常简化的例子,希望能解释我在寻找什么。
假设我有一个这样的通用类:
class MyStorage<T> {
// … (omitted members that make class invariant)
get(key: string): T {…}
set(key: string, value: T): {…}
}
我故意使这个类保持不变,所以它不能被默默地强制到更广泛的范围内T
:
const stringStorage = new MyStorage<string>(…);
// Compiler error due to type mismatch. If this were allowed, unknownStorage
// could be used to put *anything* into stringStorage.
const unknownStorage: MyStorage<unknown> = stringStorage;
这很好用,并且可以捕获由T
. 但是,有时能够保存对 MyStorage 实例的引用会很有用,T
而不必知道它的确切类型。例如,
// Made up syntax. storage is a MyStorage of some erased type that extends U.
function readsFromStorage<U>(arg: U, storage: MyStorage<? extends U>) {
// Valid. We know get() returns a type that extends U.
const value: U = storage.get('mykey');
// Compiler error. The erased T might be a narrow type than U.
storage.set('mykey', arg);
}
或者:
// Made up syntax. storage is a MyStorage of some erased type extended by U.
function writesToStorage<U>(arg: U, storage: MyStorage<U extends ?>) {
// Valid. We know that set() takes a type extended by U.
storage.set('mykey', arg);
// Compiler error. U might be narrower than the erased T type.
const value: U = storage.get('mykey');
}
通常,当另一个泛型类想要存储对 a 的引用MyStorage<T>
但不关心 的确切类型时,这将很有用T
。
这可以用今天的 TypeScript 来表达吗?如果没有,是否有现有的功能请求可以涵盖它?
解决方案
推荐阅读
- java - 如何使用 RestHighLevelClient 查找具有特定字段的文档包含字符串
- typescript - 反转在许多域类上注入依赖项的正确方法
- javascript - 如何使用 jQuery 将所有 li 元素包装在 ul 元素中?
- abap - 如何编写可调整的 ABAP 程序?
- html - SWF:重定向到本地 HTML
- java - 如何将字符串转换为 AltCase?
- javascript - ReactJS - 防止浪费渲染发生
- python - Python BeautifulSoup - 提取文本和属性值
- java - JAVA - 使用 Dijkstra 算法查找 2 个顶点之间的最短路径
- php - 如何在 Laravel 5.6 中为每个菜单使用唯一的 id 并按 id 而不是类突出显示?