If we write User, TS will find all keys assignable to the type "name" | "age" and return their value types. Instead of finding the key equal to "name" in the type User, TypeScript tries to find every key in User that is assignable to the type "name"! The real reason why it's possible to read several properties at once using a union type is because the type level expression User and the value level expression user work differently. (keyof Obj) would be incorrect because Object.keys(.) could return strings that are not assignable to keyof Obj. This is why Object.keys(.) returns a string and not a (keyof Obj) by the way. Time to practice! Check out the How Challenges Work section from Types & Values if you haven't already to learn how to take these type challenges.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |