Skip to content

Commit 7d5f7c0

Browse files
committed
implement template literal types
1 parent 98ef311 commit 7d5f7c0

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

common/corpus/expressions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ T as {} & { [t: T]: T } & { [g: G]: G }
99
---
1010

1111
(program
12-
(expression_statement (as_expression (identifier) (template_string)))
12+
(expression_statement (as_expression (identifier) (template_literal_type)))
1313
(expression_statement
1414
(as_expression
1515
(identifier)

common/corpus/types.txt

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,3 +1079,87 @@ type T<X> = T extends { x: infer X } ? X : never;
10791079
(type_annotation (infer_type (type_identifier)))))
10801080
(type_identifier)
10811081
(type_identifier))))
1082+
1083+
==================================
1084+
Template literal types
1085+
==================================
1086+
1087+
type A<B, C> = `${B}${C}`;
1088+
type A = `${B[0]}-foo-${C}-bar-${D<U, D>}`
1089+
type A = `[${'a'}${0}]`
1090+
1091+
type A<B, C> = B extends C
1092+
? C extends string
1093+
? `${C}${"" extends C ? "" : "."}${B}`
1094+
: never
1095+
: never
1096+
1097+
---
1098+
1099+
(program
1100+
(type_alias_declaration
1101+
(type_identifier)
1102+
(type_parameters
1103+
(type_parameter
1104+
(type_identifier))
1105+
(type_parameter
1106+
(type_identifier)))
1107+
(template_literal_type
1108+
(template_type
1109+
(type_identifier))
1110+
(template_type
1111+
(type_identifier))))
1112+
(type_alias_declaration
1113+
(type_identifier)
1114+
(template_literal_type
1115+
(template_type
1116+
(lookup_type
1117+
(type_identifier)
1118+
(literal_type
1119+
(number))))
1120+
(template_type
1121+
(type_identifier))
1122+
(template_type
1123+
(generic_type
1124+
(type_identifier)
1125+
(type_arguments
1126+
(type_identifier)
1127+
(type_identifier))))))
1128+
(type_alias_declaration
1129+
(type_identifier)
1130+
(template_literal_type
1131+
(template_type
1132+
(literal_type
1133+
(string)))
1134+
(template_type
1135+
(literal_type
1136+
(number)))))
1137+
(type_alias_declaration
1138+
(type_identifier)
1139+
(type_parameters
1140+
(type_parameter
1141+
(type_identifier))
1142+
(type_parameter
1143+
(type_identifier)))
1144+
(conditional_type
1145+
(type_identifier)
1146+
(type_identifier)
1147+
(conditional_type
1148+
(type_identifier)
1149+
(predefined_type)
1150+
(template_literal_type
1151+
(template_type
1152+
(type_identifier))
1153+
(template_type
1154+
(conditional_type
1155+
(literal_type
1156+
(string))
1157+
(type_identifier)
1158+
(literal_type
1159+
(string))
1160+
(literal_type
1161+
(string))))
1162+
(template_type
1163+
(type_identifier)))
1164+
(type_identifier))
1165+
(type_identifier))))

common/define-grammar.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module.exports = function defineGrammar(dialect) {
4646
[$.readonly_type, $.primary_expression],
4747
[$.type_query, $.subscript_expression, $.expression],
4848
[$.nested_type_identifier, $.generic_type, $._primary_type, $.lookup_type, $.index_type_query, $._type],
49+
[$.as_expression, $._primary_type],
4950
]),
5051

5152
conflicts: ($, previous) => previous.concat([
@@ -103,6 +104,8 @@ module.exports = function defineGrammar(dialect) {
103104
[$.array, $.tuple_type],
104105
[$.array, $.array_pattern, $.tuple_type],
105106
[$.array_pattern, $.tuple_type],
107+
108+
[$.template_literal_type, $.template_string]
106109
]),
107110

108111
inline: ($, previous) => previous
@@ -368,7 +371,7 @@ module.exports = function defineGrammar(dialect) {
368371
as_expression: $ => prec.left('binary_as', seq(
369372
$.expression,
370373
'as',
371-
choice($._type, $.template_string)
374+
choice($._type, $.template_literal_type)
372375
)),
373376

374377
class_heritage: $ => choice(
@@ -599,6 +602,18 @@ module.exports = function defineGrammar(dialect) {
599602
$.literal_type,
600603
$.lookup_type,
601604
$.conditional_type,
605+
$.template_literal_type
606+
),
607+
608+
template_type: $ => seq('${',$._primary_type,'}'),
609+
610+
template_literal_type: $ => seq(
611+
'`',
612+
repeat(choice(
613+
$._template_chars,
614+
$.template_type
615+
)),
616+
'`'
602617
),
603618

604619
infer_type: $ => seq("infer", $._type_identifier),

0 commit comments

Comments
 (0)