You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[proposal] Non widened string values should be valid enum values, like widened string valuesΒ #59346
TypeScript accepts widened strings as enum values, but refuses to accept non widened strings as enum values. I propose to relax this constraint allowing widened and non widened strings as valid enum values.
π Motivating Example
At the moment TypeScript accepts widened strings as valid enum values.
// FILE:./case1.tsexportconstCase1_ImagePngType='image/png'// Emitted in FILE:./case1.d.ts asexportdeclareconstCase1_ImagePngType="image/png"// FILE:./test1.tsimport{Case1_ImagePngType}from'./case1.js'enumCase1{// WORKS FINEPng=Case1_ImagePngType,}
Trying to use a non widened string results in a compiler error.
// FILE:./case2.tsexportconstCase2_ImageJpegType='image/jpeg'asconst// Emitted in FILE:./case2.d.ts asexportdeclareconstCase2_ImageJpegType: "image/jpeg"// FILE:./test2.tsimport{Case2_ImageJpegType}from'./case2.js'enumCase2{// FAILS WITH: Type 'string' is not assignable to type 'number' as required for computed enum member values.Jpeg=Case2_ImageJpegType,}
π» Use Cases
Given a consumer project P1 using exported widened strings values from a consumed library L1, if L1 annotates the exported strings values as const, L1 breaks P1.
Given that a widened string value is a valid enum value, a "stricter" non widened string (a subset of the widened one) should be a valid enum value too.
// FILE:./lib-1.tsexportconstImagePngType='image/png'// FILE:./lib-2.tsexportconstImageJpegType='image/jpeg'asconst// FILE:./project.tsenumMyEnum{Png=ImagePngType,// FINE.Jpeg=ImageJpegType,// SHOULD BE FINE, BUT ERROR AT THE MOMENT.}
changed the title [-]Non widened string value should be valid enum values, like widened string value[/-][+][proposal] Non widened string value should be valid enum values, like widened string value[/+]on Jul 18, 2024
changed the title [-][proposal] Non widened string value should be valid enum values, like widened string value[/-][+][proposal] Non widened string values should be valid enum values, like widened string values[/+]on Jul 18, 2024
Activity
daniele-orlando commentedon Jul 18, 2024
Follows from #59187.
[-]Non widened string value should be valid enum values, like widened string value[/-][+][proposal] Non widened string value should be valid enum values, like widened string value[/+][-][proposal] Non widened string value should be valid enum values, like widened string value[/-][+][proposal] Non widened string values should be valid enum values, like widened string values[/+]RyanCavanaugh commentedon Jul 26, 2024
Anders Hejlsberg (@ahejlsberg) this seemed like a simple oversight to me but wasn't 100% sure. Can you weigh in?
RyanCavanaugh commentedon Jul 29, 2024
Discussion notes:
const x: "foo"as a constant source for"foo"declare const x = "foo" as constin declaration files