在围绕某种已知状态使用通用包装器时,将子级强制转换为父级时遇到了不兼容的类型错误。这样的想法是,如果Tomato
扩展了Fruit
,那么Wrapper<Tomato>
和Wrapper<Fruit>
一样应该起作用。
interface Wrapper<TProps> {
// This next line is correct
mergeWith<K extends keyof TProps>(merger: (key: K) => any): void;
update<K extends keyof TProps>(updater: (value: TProps[K]) => any): void;
}
interface BrokenWrapper<TProps> {
// Somehow this next line seems identical to mergeWith above
mergeWith(merger: (key: keyof TProps) => any): void;
update<K extends keyof TProps>(updater: (value: TProps[K]) => any): void;
}