question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enum is not assignable to type string

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:7
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
SiegeSailorcommented, Mar 9, 2021

Based on my experience, It is common to use mixed-type enums, I suggest supporting it rather than reinforcing the initialization rule.

1reaction
RyanCavanaughcommented, Mar 8, 2021

Mixed-type enums aren’t actually supported; you should get an error on Name there but our initialization rules are not tight enough.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found