This has been asked a bunch of times but I'm not quite getting it. I want to have a generic type that performs some mapping from some source type to a destination type:
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'}
}
This gives me
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)
Why does this give me this error on the definition of transformFunction
?
CodePudding user response:
Oh I get it now. If it was allowed then I could call my function with a type like
type AnotherDestination = {
anotherKey: string
} & Destination
which could then not be definitely returned from the transformFunction
.