microsoft / microsoft/TypeScript

Enum is not assignable to type string

Open
#43,039 6 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Bug Report

Strings in an Enum form are not assignable to fields that require type string.

🔎 Search Terms
  • Enum as a string parameter
  • Enum is not assignable to type string
  • TS2322
🕗 Version & Regression Information
  • This is the behavior between versions 4.1.15 and 4.2.2.
⏯ Playground Link

Playground link with relevant code

💻 Code
import React from 'react'

enum Config {
    Name = 'TS',
    Width = 200,
    Height = 200
}

export default function Example(){
    return <div id={Config.Name}></div>
}
🙁 Actual behavior

The terminal is complaining with the following message:

Type 'Config' is not assignable to type 'string | undefined'.
🙂 Expected behavior

Code works without the error message.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground repro and compare its behavior between versions 4.1.15 and 4.2.2. Trace the type-checking path for the enum value passed to the JSX string attribute, then add a regression test; done means the repro no longer reports TS2322.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.