Skip to content

Backtick literals in enumerations #30962

@arcanis

Description

@arcanis

TypeScript Version: 3.4.3

Search Terms: backticks / literals / enums

Code

enum Foo {
  hello = `foo`,
};

Expected behavior:

It should work.

This is especially important because the backticks mode of the quotes rule from ESLint breaks programs (cf yarnpkg/berry#70 for an example). While it could be seen as an ESLint bug, I think it should be possible for TS to use raw template string interchangeably with regular strings as long as they don't contain variables.

Actual behavior:

It throws an error (type "foo" is not assignable to type 'Foo').

Playground Link:

https://www.typescriptlang.org/play/index.html#src=enum%20Foo%20%7B%0D%0A%20%20%20%20%20%20%20%20hello%20%3D%20%60foo%60%2C%0D%0A%7D%3B%0D%0A

Related Issues:

I think fixing this should have been part of #21476, but it focused on the case of "a" + "b" rather than the simpler case of a literal without variables.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions