Skip to content

Commit d659f9d

Browse files
committed
Generalize type queries and tuple types
* Allow arbitrary types inside of tuples * Allow typeof import('foo')
1 parent fa65017 commit d659f9d

File tree

8 files changed

+261714
-222728
lines changed

8 files changed

+261714
-222728
lines changed

common/corpus/types.txt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ type T = keyof Person<P>;
675675
type T = typeof Person;
676676
type T = typeof Person.P;
677677
type T = typeof Person<P>;
678+
type T = typeof import('person');
678679

679680
type T = keyof typeof Person;
680681

@@ -693,6 +694,8 @@ type T = keyof typeof Person;
693694
(type_query (nested_identifier (identifier) (identifier))))
694695
(type_alias_declaration (type_identifier)
695696
(type_query (generic_type (type_identifier) (type_arguments (type_identifier)))))
697+
(type_alias_declaration (type_identifier)
698+
(type_query (call_expression (import) (arguments (string)))))
696699
(type_alias_declaration (type_identifier)
697700
(index_type_query (type_query (identifier)))))
698701

@@ -851,12 +854,13 @@ Tuple types
851854
==================================
852855

853856
type t = []
854-
type t = [a, b]
855-
type t = readonly [a, b]
856-
type t = [...b]
857-
type t = [a, ...b]
858-
type t = [a, b, ...c]
859-
type t = [a, b?]
857+
type t = [A|B, C]
858+
type t = readonly [A[], B.C]
859+
860+
// special optional and rest type syntax
861+
type t = [string?, ...B]
862+
863+
// labeled elements
860864
type t = [a: A, b?: B, ...c: C[]]
861865

862866
---
@@ -868,29 +872,25 @@ type t = [a: A, b?: B, ...c: C[]]
868872
(type_alias_declaration
869873
(type_identifier)
870874
(tuple_type
871-
(identifier)
872-
(identifier)))
875+
(union_type
876+
(type_identifier)
877+
(type_identifier))
878+
(type_identifier)))
873879
(type_alias_declaration
874880
(type_identifier)
875881
(tuple_type
876882
(readonly)
877-
(identifier)
878-
(identifier)))
879-
(type_alias_declaration
880-
(type_identifier)
881-
(tuple_type (rest_identifier (identifier))))
882-
(type_alias_declaration
883-
(type_identifier)
884-
(tuple_type (identifier) (rest_identifier (identifier))))
883+
(array_type (type_identifier))
884+
(nested_type_identifier
885+
(identifier)
886+
(type_identifier))))
887+
(comment)
885888
(type_alias_declaration
886889
(type_identifier)
887890
(tuple_type
888-
(identifier)
889-
(identifier)
890-
(rest_identifier (identifier))))
891-
(type_alias_declaration
892-
(type_identifier)
893-
(tuple_type (identifier) (optional_identifier (identifier))))
891+
(optional_type (predefined_type))
892+
(rest_type (type_identifier))))
893+
(comment)
894894
(type_alias_declaration
895895
(type_identifier)
896896
(tuple_type
@@ -1002,7 +1002,7 @@ type T<X> = T extends { x: infer X } ? X : never;
10021002
(type_arguments
10031003
(infer_type (type_identifier))))
10041004
(type_identifier)
1005-
(type_identifier)))
1005+
(type_identifier)))
10061006
(type_alias_declaration
10071007
(type_identifier)
10081008
(type_parameters

common/define-grammar.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ module.exports = function defineGrammar(dialect) {
7474
[$._expression, $.generic_type],
7575
[$._expression, $.predefined_type],
7676
[$._expression, $._rest_identifier],
77-
[$._expression, $._tuple_type_identifier],
78-
[$._expression, $.optional_identifier],
77+
[$.optional_identifier, $._primary_type],
78+
[$.optional_identifier, $._primary_type, $._expression],
7979

8080
[$.object, $.object_type],
8181
[$.object, $._property_name],
@@ -530,15 +530,23 @@ module.exports = function defineGrammar(dialect) {
530530

531531
_tuple_type_identifier: $ => choice(
532532
$.identifier,
533-
$.optional_identifier,
534-
$.rest_identifier
533+
$.rest_identifier,
534+
$.optional_identifier
535535
),
536536

537-
labeled_tuple_type_member: $ => seq($._tuple_type_identifier, $.type_annotation),
537+
labeled_tuple_type_member: $ => seq(
538+
$._tuple_type_identifier,
539+
$.type_annotation
540+
),
541+
542+
optional_type: $ => seq($._type, '?'),
543+
rest_type: $ => seq('...', $._type),
538544

539545
_tuple_type_member: $ => choice(
540-
$._tuple_type_identifier,
541546
$.labeled_tuple_type_member,
547+
$.optional_type,
548+
$.rest_type,
549+
$._type,
542550
),
543551

544552
constructor_type: $ => prec.left(PREC.CONSTRUCTOR_TYPE, seq(
@@ -600,7 +608,7 @@ module.exports = function defineGrammar(dialect) {
600608

601609
type_query: $ => prec(PREC.TYPEOF, seq(
602610
'typeof',
603-
choice($.identifier, $.nested_identifier, $.generic_type)
611+
choice($.identifier, $.nested_identifier, $.generic_type, $.call_expression)
604612
)),
605613

606614
index_type_query: $ => seq(

tsx/src/grammar.json

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7859,11 +7859,11 @@
78597859
},
78607860
{
78617861
"type": "SYMBOL",
7862-
"name": "optional_identifier"
7862+
"name": "rest_identifier"
78637863
},
78647864
{
78657865
"type": "SYMBOL",
7866-
"name": "rest_identifier"
7866+
"name": "optional_identifier"
78677867
}
78687868
]
78697869
},
@@ -7880,16 +7880,50 @@
78807880
}
78817881
]
78827882
},
7883+
"optional_type": {
7884+
"type": "SEQ",
7885+
"members": [
7886+
{
7887+
"type": "SYMBOL",
7888+
"name": "_type"
7889+
},
7890+
{
7891+
"type": "STRING",
7892+
"value": "?"
7893+
}
7894+
]
7895+
},
7896+
"rest_type": {
7897+
"type": "SEQ",
7898+
"members": [
7899+
{
7900+
"type": "STRING",
7901+
"value": "..."
7902+
},
7903+
{
7904+
"type": "SYMBOL",
7905+
"name": "_type"
7906+
}
7907+
]
7908+
},
78837909
"_tuple_type_member": {
78847910
"type": "CHOICE",
78857911
"members": [
78867912
{
78877913
"type": "SYMBOL",
7888-
"name": "_tuple_type_identifier"
7914+
"name": "labeled_tuple_type_member"
78897915
},
78907916
{
78917917
"type": "SYMBOL",
7892-
"name": "labeled_tuple_type_member"
7918+
"name": "optional_type"
7919+
},
7920+
{
7921+
"type": "SYMBOL",
7922+
"name": "rest_type"
7923+
},
7924+
{
7925+
"type": "SYMBOL",
7926+
"name": "_type"
78937927
}
78947928
]
78957929
},
@@ -8155,6 +8189,10 @@
81558189
{
81568190
"type": "SYMBOL",
81578191
"name": "generic_type"
8192+
},
8193+
{
8194+
"type": "SYMBOL",
8195+
"name": "call_expression"
81588196
}
81598197
]
81608198
}
@@ -9322,12 +9360,13 @@
93229360
"_rest_identifier"
93239361
],
93249362
[
9325-
"_expression",
9326-
"_tuple_type_identifier"
9363+
"optional_identifier",
9364+
"_primary_type"
93279365
],
93289366
[
9329-
"_expression",
9330-
"optional_identifier"
9367+
"optional_identifier",
9368+
"_primary_type",
9369+
"_expression"
93319370
],
93329371
[
93339372
"object",

0 commit comments

Comments
 (0)