Skip to content

Commit 6cb04ce

Browse files
Add support for extends clauses in infer
1 parent ff072b8 commit 6cb04ce

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

common/corpus/types.txt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,7 @@ type F<T, X, Y> = (t: T) => X extends Y ? X : Y extends (t: T) => X extends Y ?
11201120
type T<X, Y> = T extends X<infer Y> ? Y : X
11211121
type T<X> = X extends (infer X)[] ? X : never;
11221122
type T<X> = T extends { x: infer X } ? X : never;
1123+
type T<X> = T extends { x: infer X extends number } ? X : never;
11231124

11241125
---
11251126
(program
@@ -1229,6 +1230,18 @@ type T<X> = T extends { x: infer X } ? X : never;
12291230
(property_identifier)
12301231
(type_annotation (infer_type (type_identifier)))))
12311232
(type_identifier)
1233+
(predefined_type)))
1234+
(type_alias_declaration
1235+
(type_identifier)
1236+
(type_parameters
1237+
(type_parameter (type_identifier)))
1238+
(conditional_type
1239+
(type_identifier)
1240+
(object_type
1241+
(property_signature
1242+
(property_identifier)
1243+
(type_annotation (infer_type (type_identifier) (predefined_type)))))
1244+
(type_identifier)
12321245
(predefined_type))))
12331246

12341247
==================================
@@ -1245,6 +1258,7 @@ type A<B, C> = B extends C
12451258
: never
12461259
type Trim<S extends string> = S extends `${infer R}` ? Trim<R> : S;
12471260
type A = `${true & ('foo' | false)}`;
1261+
type StringToNumber<S extends string> = S extends `${infer N extends number}` ? N : never;
12481262
---
12491263
(program
12501264
(type_alias_declaration
@@ -1346,7 +1360,23 @@ type A = `${true & ('foo' | false)}`;
13461360
(string
13471361
(string_fragment)))
13481362
(literal_type
1349-
(false)))))))))
1363+
(false))))))))
1364+
(type_alias_declaration
1365+
(type_identifier)
1366+
(type_parameters
1367+
(type_parameter
1368+
(type_identifier)
1369+
(constraint
1370+
(predefined_type))))
1371+
(conditional_type
1372+
(type_identifier)
1373+
(template_literal_type
1374+
(template_type
1375+
(infer_type
1376+
(type_identifier)
1377+
(predefined_type))))
1378+
(type_identifier)
1379+
(predefined_type))))
13501380

13511381
==================================
13521382
Mapped type 'as' clauses

common/define-grammar.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,14 @@ module.exports = function defineGrammar(dialect) {
707707
'`'
708708
),
709709

710-
infer_type: $ => seq("infer", $._type_identifier),
710+
infer_type: $ => prec.right(seq(
711+
'infer',
712+
$._type_identifier,
713+
optional(seq(
714+
'extends',
715+
$._type
716+
))
717+
)),
711718

712719
conditional_type: $ => prec.left(seq(
713720
field('left', $._type),

0 commit comments

Comments
 (0)