可以用不同的约束子类型实例化

could be instantiated with a different subtype of constraint

这个问题已经被问过很多次了,但我不太明白。我想要一个通用类型来执行从某个源类型到目标类型的映射:

type Source = {
    valSource: string
}

type Destination = {
    valDest: string
}

type GenericTransform = <S extends Source, D extends Destination>(source: S) => D

const transformFunction: GenericTransform = (s: Source): Destination => {
    return {valDest: 'value for dest'}
}

这给了我

Type '(s: Source) => Destination' is not assignable to type 'TransformInterface'.
    Type 'Destination' is not assignable to type 'D'.
        'Destination' is assignable to the constraint of type 'D', but 'D' could be instantiated with a different subtype of constraint 'Destination'.(2322)

为什么这会给我关于 transformFunction 的定义的错误?

哦,我明白了。如果允许,那么我可以用

这样的类型调用我的函数
type AnotherDestination = {
    anotherKey: string
} & Destination

然后无法从 transformFunction.

中明确返回