Skip to content

string enum works from exported strings but not from strings exported as constΒ #59187

Closed as not planned
@daniele-orlando

Description

@daniele-orlando

πŸ”Ž Search Terms

enum

πŸ•— Version & Regression Information

TypeScript: 5.5.3

  • This is the behavior in every version I tried

⏯ Playground Link

Reproduction Project

πŸ’» Code

// FILE: ./case1.ts
export const Case1_ImageJpegType = 'image/jpeg'
export const Case1_ImagePngType = 'image/png'

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


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

enum Case1 {
  // WORKS AS EXPECTED
  Png = Case1_ImagePngType,
  Jpeg = Case1_ImageJpegType,
}

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

enum Case2 {
  // FAILS WITH: Type 'string' is not assignable to type 'number' as required for computed enum member values.
  Png = Case2_ImagePngType,
  Jpeg = Case2_ImageJpegType,
}

πŸ™ Actual behavior

Exporting a string as const breaks string enums.

npx -p [email protected] tsc --noEmit -p tsconfig.verbatime-false.json
// FAILS with
// Type 'string' is not assignable to type 'number' as required for computed enum member values.

πŸ™‚ Expected behavior

Exporting a string with or without as const should not break string enums.

npx -p [email protected] tsc --noEmit -p tsconfig.verbatime-false.json
// SHOULD NOT FAIL
export const MyConst = 'MyConst'
export const MyConst = 'MyConst' as const

should behave the same when used as

import {MyConst} from './something'

enum MyEnum {
  MyType = MyConst,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions