Skip to content

Template strings in types are buggy #206

Closed
@DGCK81LNN

Description

@DGCK81LNN

The following piece of code is valid but it is parsed incorrectly:

var this_ = ``;
var isCorrect = `foo`;
type ButThis = ``; //<- first backtick is misparsed
type IsWrong = `foo`;

// Um... So I'm in a template string or what?

var value = `type A = string`; //<- second backtick is misparsed

The snippet above is valid TypeScript:
https://www.typescriptlang.org/play?#code/G4QwTgBALgFglgZwPoQLwQAYYNwChSSIDCA9mGAKYDGUamAZiSTrlAJ4AOFEAQgK5QAKvAR0s2CAHpJAHgC0EenDAJaAIxBUA1lDjaIiCAHd4UCq07cAkggDqYEgDsA5mMbM8uaRACqAWwA6IIgAZRIIKwByPwNHCBBoCj8OABsQMwhVMDgXCDJjGHSAflx8cAhQFL5udAx2LggAQTosnOccKVkFBGonABMIDW1dfUMTODMgA

I was just browsing someone's code on GitHub when I noticed the highlighting was wrong. I tried to reproduce the problem in the snippet above. The first backtick on line 3 seems to have been ignored or what.

The original code I saw looked more like this, actually:

type A<B extends string, C>
  = B extends `${any}:${infer T}` ? T extends D ? E[T] : F : F
type G<B extends string, C>
  = B extends `${infer L}${X}${infer R}` ? `${L}${Y}${Replace<R, X, Y>}` : B
type H<B extends string, C>
  = B extends `${infer L}]${infer R}` ? [ParamType<L, K>, ...ExtractAll<R, K>] : []

The output of tree-sitter parse is the following: I didn't know anything about tree-sitter before, and I have been unable to run tree-sitter right now, sorry. I'll come back to this later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions