Skip to content

Commit d08f57f

Browse files
authored
Merge pull request #138 from resolritter/template_literal
"Template literal types" and "Mapped type 'as' clauses"
2 parents 98ef311 + 5978311 commit d08f57f

File tree

11 files changed

+432815
-416768
lines changed

11 files changed

+432815
-416768
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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,3 +1079,146 @@ 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+
type A<B, C> = B extends C
1091+
? C extends string
1092+
? `${C}${"" extends C ? "" : "."}${B}`
1093+
: never
1094+
: never
1095+
1096+
---
1097+
1098+
(program
1099+
(type_alias_declaration
1100+
(type_identifier)
1101+
(type_parameters
1102+
(type_parameter
1103+
(type_identifier))
1104+
(type_parameter
1105+
(type_identifier)))
1106+
(template_literal_type
1107+
(template_type
1108+
(type_identifier))
1109+
(template_type
1110+
(type_identifier))))
1111+
(type_alias_declaration
1112+
(type_identifier)
1113+
(template_literal_type
1114+
(template_type
1115+
(lookup_type
1116+
(type_identifier)
1117+
(literal_type
1118+
(number))))
1119+
(template_type
1120+
(type_identifier))
1121+
(template_type
1122+
(generic_type
1123+
(type_identifier)
1124+
(type_arguments
1125+
(type_identifier)
1126+
(type_identifier))))))
1127+
(type_alias_declaration
1128+
(type_identifier)
1129+
(template_literal_type
1130+
(template_type
1131+
(literal_type
1132+
(string (string_fragment))))
1133+
(template_type
1134+
(literal_type
1135+
(number)))))
1136+
(type_alias_declaration
1137+
(type_identifier)
1138+
(type_parameters
1139+
(type_parameter
1140+
(type_identifier))
1141+
(type_parameter
1142+
(type_identifier)))
1143+
(conditional_type
1144+
(type_identifier)
1145+
(type_identifier)
1146+
(conditional_type
1147+
(type_identifier)
1148+
(predefined_type)
1149+
(template_literal_type
1150+
(template_type
1151+
(type_identifier))
1152+
(template_type
1153+
(conditional_type
1154+
(literal_type
1155+
(string))
1156+
(type_identifier)
1157+
(literal_type
1158+
(string))
1159+
(literal_type
1160+
(string (string_fragment)))))
1161+
(template_type
1162+
(type_identifier)))
1163+
(type_identifier))
1164+
(type_identifier))))
1165+
1166+
==================================
1167+
Mapped type 'as' clauses
1168+
==================================
1169+
1170+
type A<B> = { [B in keyof C & string as `get${Capitalize<P>}`]: () => A[B] };
1171+
type A<B> = { [B in keyof C & string as `${P}1` | `${P}2`]: A[B] }
1172+
1173+
---
1174+
1175+
(program
1176+
(type_alias_declaration
1177+
(type_identifier)
1178+
(type_parameters
1179+
(type_parameter
1180+
(type_identifier)))
1181+
(object_type
1182+
(index_signature
1183+
(mapped_type_clause
1184+
(type_identifier)
1185+
(intersection_type
1186+
(index_type_query
1187+
(type_identifier))
1188+
(predefined_type))
1189+
(template_literal_type
1190+
(template_type
1191+
(generic_type
1192+
(type_identifier)
1193+
(type_arguments
1194+
(type_identifier))))))
1195+
(type_annotation
1196+
(function_type
1197+
(formal_parameters)
1198+
(lookup_type
1199+
(type_identifier)
1200+
(type_identifier)))))))
1201+
(type_alias_declaration
1202+
(type_identifier)
1203+
(type_parameters
1204+
(type_parameter
1205+
(type_identifier)))
1206+
(object_type
1207+
(index_signature
1208+
(mapped_type_clause
1209+
(type_identifier)
1210+
(intersection_type
1211+
(index_type_query
1212+
(type_identifier))
1213+
(predefined_type))
1214+
(union_type
1215+
(template_literal_type
1216+
(template_type
1217+
(type_identifier)))
1218+
(template_literal_type
1219+
(template_type
1220+
(type_identifier)))))
1221+
(type_annotation
1222+
(lookup_type
1223+
(type_identifier)
1224+
(type_identifier)))))))

common/define-grammar.js

Lines changed: 17 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),
@@ -652,6 +667,7 @@ module.exports = function defineGrammar(dialect) {
652667
$._type_identifier,
653668
'in',
654669
$._type,
670+
optional(seq('as', $._type))
655671
),
656672

657673
literal_type: $ => choice(

tsx/src/grammar.json

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7326,7 +7326,7 @@
73267326
},
73277327
{
73287328
"type": "SYMBOL",
7329-
"name": "template_string"
7329+
"name": "template_literal_type"
73307330
}
73317331
]
73327332
}
@@ -8467,6 +8467,56 @@
84678467
{
84688468
"type": "SYMBOL",
84698469
"name": "conditional_type"
8470+
},
8471+
{
8472+
"type": "SYMBOL",
8473+
"name": "template_literal_type"
8474+
}
8475+
]
8476+
},
8477+
"template_type": {
8478+
"type": "SEQ",
8479+
"members": [
8480+
{
8481+
"type": "STRING",
8482+
"value": "${"
8483+
},
8484+
{
8485+
"type": "SYMBOL",
8486+
"name": "_primary_type"
8487+
},
8488+
{
8489+
"type": "STRING",
8490+
"value": "}"
8491+
}
8492+
]
8493+
},
8494+
"template_literal_type": {
8495+
"type": "SEQ",
8496+
"members": [
8497+
{
8498+
"type": "STRING",
8499+
"value": "`"
8500+
},
8501+
{
8502+
"type": "REPEAT",
8503+
"content": {
8504+
"type": "CHOICE",
8505+
"members": [
8506+
{
8507+
"type": "SYMBOL",
8508+
"name": "_template_chars"
8509+
},
8510+
{
8511+
"type": "SYMBOL",
8512+
"name": "template_type"
8513+
}
8514+
]
8515+
}
8516+
},
8517+
{
8518+
"type": "STRING",
8519+
"value": "`"
84708520
}
84718521
]
84728522
},
@@ -8680,6 +8730,27 @@
86808730
{
86818731
"type": "SYMBOL",
86828732
"name": "_type"
8733+
},
8734+
{
8735+
"type": "CHOICE",
8736+
"members": [
8737+
{
8738+
"type": "SEQ",
8739+
"members": [
8740+
{
8741+
"type": "STRING",
8742+
"value": "as"
8743+
},
8744+
{
8745+
"type": "SYMBOL",
8746+
"name": "_type"
8747+
}
8748+
]
8749+
},
8750+
{
8751+
"type": "BLANK"
8752+
}
8753+
]
86838754
}
86848755
]
86858756
},
@@ -9835,6 +9906,10 @@
98359906
[
98369907
"array_pattern",
98379908
"tuple_type"
9909+
],
9910+
[
9911+
"template_literal_type",
9912+
"template_string"
98389913
]
98399914
],
98409915
"precedences": [
@@ -10203,6 +10278,16 @@
1020310278
"type": "SYMBOL",
1020410279
"name": "_type"
1020510280
}
10281+
],
10282+
[
10283+
{
10284+
"type": "SYMBOL",
10285+
"name": "as_expression"
10286+
},
10287+
{
10288+
"type": "SYMBOL",
10289+
"name": "_primary_type"
10290+
}
1020610291
]
1020710292
],
1020810293
"externals": [

0 commit comments

Comments
 (0)