Enum is not assignable to type string
See original GitHub issueBug 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
and4.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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Cannot assign a string value to typescript enum (Initializer ...
it seems to me that no answer provides explanation to why this won't work. From a similar issue on github: "The intent of...
Read more >Enum value is not assignable to type "string", even though it ...
In this case: Enum1 and Enum2 are not compatible to each other. But each enum should be compatible to a string variable. Another...
Read more >TypeScript string enums, and when and how to use them
No matter how much you know about TypeScript enums, this guide covers all the best practices for using string-based enums in production.
Read more >Handbook - Enums - TypeScript
TypeScript provides both numeric and string-based enums. ... Square' is not assignable to type 'ShapeKind.Circle'.2322Type 'ShapeKind.
Read more >Enums - TypeScript Deep Dive - Gitbook
Here is an example definition of a TypeScript enum: ... card = "not a member of card suit"; // Error : string is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Based on my experience, It is common to use mixed-type enums, I suggest supporting it rather than reinforcing the initialization rule.
Mixed-type enums aren’t actually supported; you should get an error on
Name
there but our initialization rules are not tight enough.