Skip to content

Parse backtick delineated string without interpolation as string literals #18109

Description

@ericbf

TypeScript Version: 2.4.0

Code

let test: `a` | `b`

Expected behavior:
test will be a variable of type "a" | "b", like when single quotes or double quotes are used.

Actual behavior:
It does not compile, giving various errors.

Justification:
We like to enforce the use of template strings instead of concatenation, but that leaves us with a mixture of backtick strings and either double or single quote strings. Ideally we would enforce just one type of quote throughout our projects and leave it at that. In order to utilize the power of template strings, we should obviously enforce only backticks for strings.

The problem now is that the compiler doesn't accurately parse strings delineated by backticks. When backtick delineated strings don't have any interpolation in them, the compiler should be able to recognize that they are just literal strings and should use them for type inference and everything else.

Activity

  1. changed the title [-]Parse backticks correctly when using for all strings (for consistency)[/-] [+]Parse backtick delineated string without interpolation as string literals[/+] on Aug 29, 2017
  2. ericbf commented on Sep 1, 2017

    @ericbf
    ContributorAuthor

    I'd argue that this is a duplicate of #16592 and that that particular issue wasn't fully fixed by #17704.

    Daniel Rosenwasser (@DanielRosenwasser)

    We should just allow people to use NoSubstitutionTemplates the way that they're allowed to use regular strings.

    That also includes being able to put NoSubstitutionTemplates as types, and anywhere else that string literals can be used.

  3. added
    DuplicateAn existing issue was already created
    and removed
    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
    Domain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefined
    SuggestionAn idea for TypeScript
    on Sep 5, 2017
  4. mhegazy commented on Sep 5, 2017

    @mhegazy
    Contributor

    closing in favor of #16592. thanks.

  5. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions