Mat*_*tir 5 javascript types typescript
打字稿为此代码引发错误(抽象 - 不是真正的代码):
export const enum typeList {
a = "aa",
b = "bb",
c = "cc"
}
export interface parameterEvent<S extends typeList> {
"kind": S;
"detail": DetailTypes[S];
}
export interface DetailTypes {
[typeList.a]: string;
[typeList.b]: boolean;
[typeList.c]: undefined;
}
interface returnTypes {
[typeList.a]: number | string;
[typeList.b]: number;
[typeList.c]: boolean;
}
type Universial<T extends typeList> = (event: parameterEvent<T>) => returnTypes[T];
type collection = {
[T in typeList]: Universial<T>[];
};
const universialGroup: collection = {
[typeList.a]: [],
[typeList.b]: [],
[typeList.c]: []
};
export function addToUniversalGroup<T extends typeList>(kind: T, val: Universial<T>): void {
const listeners = universialGroup[kind] as Universial<T>[];
}
Run Code Online (Sandbox Code Playgroud)
错误:
将类型 'collection[T]' 转换为类型 'Universial[]' 可能是错误的,因为这两种类型与另一种类型都没有足够的重叠。如果这是故意的,请先将表达式转换为“未知”。输入'通用<typeList.a>[] | 通用<typeList.b>[] | Universial<typeList.c>[]' 不能与类型 'Universial[]' 进行比较。类型 'Universial<typeList.c>[]' 不能与类型 'Universial[]' 相比。类型 'Universial<typeList.c>' 不能与类型 'Universial' 相比。类型 'boolean' 不能与类型 'returnTypes[T]' 进行比较。
一个有趣的事实是,event: parameterEvent<T>
从 Universal 类型中删除可以修复这个错误,恕我直言,这没有意义。
我想了解这个例子有什么问题?为什么我会收到此错误消息?
归档时间: |
|
查看次数: |
86 次 |
最近记录: |