Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[proposal] Non widened string values should be valid enum values, like widened string values #59346

Open
6 tasks done
daniele-orlando opened this issue Jul 18, 2024 · 3 comments Β· May be fixed by #59475
Open
6 tasks done
Assignees
Labels
Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript

Comments

@daniele-orlando
Copy link

daniele-orlando commented Jul 18, 2024

πŸ” Search Terms

enum, string

βœ… Viability Checklist

⭐ Suggestion

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.ts
export const Case1_ImagePngType = 'image/png'

// Emitted in FILE:./case1.d.ts as
export declare const Case1_ImagePngType = "image/png"

// FILE:./test1.ts
import {Case1_ImagePngType} from './case1.js'

enum Case1 {
  // WORKS FINE
  Png = Case1_ImagePngType,
}

Trying to use a non widened string results in a compiler error.

// FILE:./case2.ts
export const Case2_ImageJpegType = 'image/jpeg' as const

// Emitted in FILE:./case2.d.ts as
export declare const Case2_ImageJpegType: "image/jpeg"

// FILE:./test2.ts
import {Case2_ImageJpegType} from './case2.js'

enum Case2 {
  // 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.ts
export const ImagePngType = 'image/png'

// FILE:./lib-2.ts
export const ImageJpegType = 'image/jpeg' as const

// FILE:./project.ts
enum MyEnum {
  Png = ImagePngType, // FINE.
  Jpeg = ImageJpegType, // SHOULD BE FINE, BUT ERROR AT THE MOMENT.
}
@daniele-orlando
Copy link
Author

Follows from #59187.

@daniele-orlando daniele-orlando 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 Jul 18, 2024
@daniele-orlando daniele-orlando 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 Jul 18, 2024
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jul 26, 2024
@RyanCavanaugh
Copy link
Member

@ahejlsberg this seemed like a simple oversight to me but wasn't 100% sure. Can you weigh in?

@RyanCavanaugh
Copy link
Member

Discussion notes:

  • Allow interpreting const x: "foo" as a constant source for "foo"
  • Allow seeing declare const x = "foo" as const in declaration files
    • Eventually change the declaration emitter to use that format instead

@ahejlsberg ahejlsberg added Suggestion An idea for TypeScript Fix Available A PR has been opened for this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels Jul 30, 2024
@ahejlsberg ahejlsberg added this to the TypeScript 5.7.0 milestone Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript
Projects
None yet
3 participants