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 — Intersection on indexing a conditional type object · Issue #55804 · microsoft/TypeScript Closed Labels Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed Description🔎 Search Terms"indexing a conditional type object", "type mismatch indexing a conditional type object ts" 🕗 Version & Regression Information
⏯ Playground Link💻 Codeexport const KEYS = ['cat', 'dog', 'fox', 'orca', 'ox'] as const export const KEYS_TYPE_MAPPING = { cat: 'options', dog: 'options', fox: 'options', orca: 'range', ox: 'range', } as const export type KeyValue = { options: string[] range: { min?: number; max?: number } } export type Items = { [K in (typeof KEYS)[number]]?: KeyValue[(typeof KEYS_TYPE_MAPPING)[K]] } let animals: Items = {cat: ["x", "y", "x"]} for (let key of KEYS) { // union of possible value types in 'KeyValue' let x = animals[key] // ERROR: becomes an intersection when trying to set the value animals[key] = x } 🙁 Actual behaviorAfter creating a object type where each key has a mapped value type, an intersection of said types is created which causes object[key] to not be the same type as object[key] when setting a value at an index of the object. It should not be an intersection type since the value types never overlap. 🙂 Expected behaviorI expected the type of 'x' (when setting it as the value of object[key]) to be the same type as 'object[key]'. Since they both reference the same index in the same object. Also I would expect the type of 'x' and 'object[key]' to be a union of the value types since they do not overlap. Additional information about the issueNo response Activity
MetadataMetadataAssigneesLabelsDesign LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed 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. |