Skip to content

Commit

Permalink
Support "infer" types (#124)
Browse files Browse the repository at this point in the history
* support inferred types

* regenerate parser for 'infer_conditional_types'

Co-authored-by: resolritter <resolritter@users.noreply.github.com>
  • Loading branch information
resolritter and resolritter committed Feb 5, 2021
1 parent c17fe89 commit da72060
Show file tree
Hide file tree
Showing 8 changed files with 191,675 additions and 189,574 deletions.
40 changes: 39 additions & 1 deletion common/corpus/types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,9 @@ type T = X extends Y ? Z : Y
type T = X extends ?Y ? ?X : Y
type F<T, X, Y> = ((t: T) => X extends Y ? X : Y) extends ((t: T) => X extends Y ? Y : X) ? X : Y
type F<T, X, Y> = (t: T) => X extends Y ? X : Y extends (t: T) => X extends Y ? Y : X ? X : Y
type T<X, Y> = T extends X<infer Y> ? Y : X
type T<X> = X extends (infer X)[] ? X : never;
type T<X> = T extends { x: infer X } ? X : never;

---
(program
Expand Down Expand Up @@ -986,4 +989,39 @@ type F<T, X, Y> = (t: T) => X extends Y ? X : Y extends (t: T) => X extends Y ?
(type_identifier)
(type_identifier)))
(type_identifier)
(type_identifier)))))
(type_identifier))))
(type_alias_declaration
(type_identifier)
(type_parameters
(type_parameter (type_identifier))
(type_parameter (type_identifier)))
(conditional_type
(type_identifier)
(generic_type
(type_identifier)
(type_arguments
(infer_type (type_identifier))))
(type_identifier)
(type_identifier)))
(type_alias_declaration
(type_identifier)
(type_parameters
(type_parameter (type_identifier)))
(conditional_type
(type_identifier)
(array_type
(parenthesized_type (infer_type (type_identifier))))
(type_identifier)
(type_identifier)))
(type_alias_declaration
(type_identifier)
(type_parameters
(type_parameter (type_identifier)))
(conditional_type
(type_identifier)
(object_type
(property_signature
(property_identifier)
(type_annotation (infer_type (type_identifier)))))
(type_identifier)
(type_identifier))))
5 changes: 4 additions & 1 deletion common/define-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,8 @@ module.exports = function defineGrammar(dialect) {
$.union_type,
$.intersection_type,
$.function_type,
$.constructor_type
$.constructor_type,
$.infer_type
),

optional_identifier: $ => seq($.identifier, '?'),
Expand Down Expand Up @@ -567,6 +568,8 @@ module.exports = function defineGrammar(dialect) {
$.conditional_type,
),

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

conditional_type: $ => prec.left(PREC.CONDITIONAL_TYPE, seq(
field('left', $._type),
'extends',
Expand Down
17 changes: 17 additions & 0 deletions tsx/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7830,6 +7830,10 @@
{
"type": "SYMBOL",
"name": "constructor_type"
},
{
"type": "SYMBOL",
"name": "infer_type"
}
]
},
Expand Down Expand Up @@ -7995,6 +7999,19 @@
}
]
},
"infer_type": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "infer"
},
{
"type": "SYMBOL",
"name": "_type_identifier"
}
]
},
"conditional_type": {
"type": "PREC_LEFT",
"value": 2,
Expand Down
115 changes: 115 additions & 0 deletions tsx/src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -807,6 +811,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -898,6 +906,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -1565,6 +1577,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -1651,6 +1667,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -1737,6 +1757,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -1823,6 +1847,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -1915,6 +1943,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -2033,6 +2065,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -2172,6 +2208,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -2997,6 +3037,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -3279,6 +3323,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -3511,6 +3559,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -3609,6 +3661,21 @@
]
}
},
{
"type": "infer_type",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "type_identifier",
"named": true
}
]
}
},
{
"type": "interface_declaration",
"named": true,
Expand Down Expand Up @@ -3729,6 +3796,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -4199,6 +4270,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -4290,6 +4365,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -4931,6 +5010,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -5022,6 +5105,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -5235,6 +5322,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -5964,6 +6055,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -6056,6 +6151,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -6147,6 +6246,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -6280,6 +6383,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -6459,6 +6566,10 @@
"type": "index_type_query",
"named": true
},
{
"type": "infer_type",
"named": true
},
{
"type": "intersection_type",
"named": true
Expand Down Expand Up @@ -7038,6 +7149,10 @@
"type": "in",
"named": false
},
{
"type": "infer",
"named": false
},
{
"type": "instanceof",
"named": false
Expand Down

0 comments on commit da72060

Please sign in to comment.