Bug Report
🔎 Search Terms
infer contextual string union
🕗 Version & Regression Information
- This changed between versions 5.1.3 and 5.0.4
⏯ Playground Link
Playground link with relevant code
💻 Code
exportinterfaceUnionAltA{type?: 'text';}exportinterfaceUnionAltB{type?: 'image'|'video'|'document';}exporttypeValueUnion=UnionAltA|UnionAltB;exporttypeMapOrSingleton=|{[key: string]: ValueUnion;}|ValueUnion;constwithoutAsConst: MapOrSingleton={1: {type: 'text'/*as const*/,},};constwithAsConst: MapOrSingleton={1: {type: 'text'asconst,},};🙁 Actual behavior
The declaration of withoutAsConst fails, as type: "text" fails to be inferred as "text", and instead infers as string in this situation:
Type 'string' is not assignable to type '"text" | "image" | "video" | "document" | undefined'.
🙂 Expected behavior
The declaration of withoutAsConst should succeed. Typescript understands that the only valid values in this position are "text" | "image" | "video" | "document" | undefined from the contextual type, so the literal "text" should match that.
Bug Report
🔎 Search Terms
infer contextual string union
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The declaration of
withoutAsConstfails, astype: "text"fails to be inferred as"text", and instead infers asstringin this situation:🙂 Expected behavior
The declaration of
withoutAsConstshould succeed. Typescript understands that the only valid values in this position are"text" | "image" | "video" | "document" | undefinedfrom the contextual type, so the literal"text"should match that.