Skip to content

Commit 012ea5a

Browse files
committed
implement "mapped type 'as' clauses"
1 parent 7d5f7c0 commit 012ea5a

File tree

2 files changed

+87
-27
lines changed

2 files changed

+87
-27
lines changed

common/corpus/types.txt

Lines changed: 86 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,6 @@ Template literal types
10871087
type A<B, C> = `${B}${C}`;
10881088
type A = `${B[0]}-foo-${C}-bar-${D<U, D>}`
10891089
type A = `[${'a'}${0}]`
1090-
10911090
type A<B, C> = B extends C
10921091
? C extends string
10931092
? `${C}${"" extends C ? "" : "."}${B}`
@@ -1134,32 +1133,92 @@ type A<B, C> = B extends C
11341133
(template_type
11351134
(literal_type
11361135
(number)))))
1137-
(type_alias_declaration
1138-
(type_identifier)
1139-
(type_parameters
1140-
(type_parameter
1141-
(type_identifier))
1142-
(type_parameter
1143-
(type_identifier)))
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
11441153
(conditional_type
1154+
(literal_type
1155+
(string))
11451156
(type_identifier)
1146-
(type_identifier)
1147-
(conditional_type
1157+
(literal_type
1158+
(string))
1159+
(literal_type
1160+
(string))))
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
11481192
(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))))
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ module.exports = function defineGrammar(dialect) {
667667
$._type_identifier,
668668
'in',
669669
$._type,
670+
optional(seq('as', $._type))
670671
),
671672

672673
literal_type: $ => choice(

0 commit comments

Comments
 (0)