当在某种已知状态周围使用通用包装器时,我在将子项转换为其父项时遇到了不兼容的类型错误。这个想法是如果Tomato
extends 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;
}