類型
要替換掉的類型
要替換成的類型
class Dog { bark() {} };
class Cat { meow() {} };
type CatFan = { pet: Cat, friend: { pet: Cat } };
type DogFan = ReplaceType<CatFan, Cat, Dog> // => { pet: Dog, friend: { pet: Dog } }
const dogOwner = { pet: new Dog(), friend: { pet: new Cat() } };
dogOwner satisfies CatFan; // ❌ Error: Dog is not assignable to type Cat
dogOwner satisfies DogFan; // ❌ Error: Cat is not assignable to type Dog
const dogFan = { pet: new Dog(), friend: { pet: new Dog() } };
dogFan satisfies DogFan; // ✅
Generated using TypeDoc
TypeScript 類型,把
T
所有類型為TFrom
的成員替換成TTo
所有層級都會替換。
TFrom
必須和TTo
不相容