GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Conditional type inference regression in 4.2* · Issue #43192 · microsoft/TypeScript Closed Labels Breaking ChangeWould introduce errors in existing codeWould introduce errors in existing codeWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug DescriptionBug Report🔎 Search Terms"conditional type inference" 🕗 Version & Regression InformationThis changed between versions 4.1.5 and 4.2.2 ⏯ Playground LinkPlayground link with relevant code 💻 Codeexport interface IFruits { tropical: { papaya: number; pineapple: number; guava: number; }; stone: { peach: number; nectarine: number; }; berry: { strawberry: number; blueberry: number; }; } /** * Aggregating all the second level keys (the fruits) into a single union type. */ export type FruitKey = { [category in keyof IFruits]: keyof IFruits[category]; }[keyof IFruits]; /** * Helper to pick only fruit categories (first level) that are required for the requested fruit keys. */ type RequiredFruitCategories<K extends FruitKey> = { [category in keyof IFruits]: Extract<K, keyof IFruits[category]> extends never ? never : category; }[keyof IFruits]; /** * Takes a nested slice of the IFruits interface depending on the fruit keys (second level) that * we ask for -- essentially a nested `Pick`. So `FruitsSubset<"papaya" | "peach">` ends up being: * { * tropical: { * papaya: number; * }; * stone: { * peach: number; * }; * } */ type FruitsSubset<K extends FruitKey> = { [category in RequiredFruitCategories<K>]: Pick< IFruits[category], Extract<K, keyof IFruits[category]> >; }; /** * Just wrapping the above in an interface for other interfaces (i.e. React props) to extend. */ interface FruitsSubsetOpts<K extends FruitKey> { fruits: FruitsSubset<K>; } type MyFavoriteFruitsSubset = FruitsSubset<"papaya" | "peach">; // Inferring the key from above -- this ends up being `"papaya" | "peach"` on TS 4.1.5 but `never` on 4.2.3 type MyFavoriteFruitsSubsetKey = MyFavoriteFruitsSubset extends FruitsSubset<infer K> ? K : never; type MyFavoriteFruitsSubsetOpts = FruitsSubsetOpts<"papaya" | "peach">; // Inferring the key from above -- this ends up being `"papaya" | "peach"` on both TS 4.1.5 and 4.2.3 // Pretty confusing because FruitsSubsetOpts is just a wrapper around FruitsSubset! type MyFavoriteFruitsSubsetOptsKey = MyFavoriteFruitsSubsetOpts extends FruitsSubsetOpts<infer K> ? K : never; 🙁 Actual behavior
🙂 Expected behavior
Reacted by Val Kharitonov Activity
MetadataMetadataAssigneesLabelsBreaking ChangeWould introduce errors in existing codeWould introduce errors in existing codeWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |