這已經被問了很多次,但我不太明白。我想要一個泛型型別,它執行從某個源型別到目標型別的一些映射:
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?
uj5u.com熱心網友回復:
哦,我現在明白了。如果允許,那么我可以使用類似的型別呼叫我的函式
type AnotherDestination = {
anotherKey: string
} & Destination
然后不能肯定地從transformFunction.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/437878.html
