Skip to content

Commit 2097b39

Browse files
committed
Merge remote-tracking branch 'resolritter/conditional_types_2'
2 parents 9f75334 + c6d56e7 commit 2097b39

File tree

2 files changed

+133
-14
lines changed

2 files changed

+133
-14
lines changed

common/corpus/types.txt

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,10 @@ Assertion functions checking a type
745745
function f(x: any): asserts x is number {
746746
}
747747

748+
function isT(t: T): t is T {
749+
return true
750+
}
751+
748752
---
749753

750754
(program
@@ -753,8 +757,19 @@ function f(x: any): asserts x is number {
753757
(formal_parameters
754758
(required_parameter
755759
(identifier) (type_annotation (predefined_type))))
756-
(asserts (identifier) (predefined_type))
757-
(statement_block)))
760+
(asserts (type_predicate (identifier) (predefined_type)))
761+
(statement_block))
762+
(function_declaration
763+
(identifier)
764+
(formal_parameters
765+
(required_parameter
766+
(identifier)
767+
(type_annotation (type_identifier))))
768+
(type_predicate_annotation
769+
(type_predicate
770+
(identifier)
771+
(type_identifier)))
772+
(statement_block (return_statement (true)))))
758773

759774
==================================
760775
Read-only arrays
@@ -857,3 +872,87 @@ type t = [a: A, b?: B, ...c: C[]]
857872
(labeled_tuple_type_member
858873
(rest_identifier (identifier))
859874
(type_annotation (array_type (type_identifier)))))))
875+
876+
==================================
877+
Conditional types
878+
==================================
879+
880+
type T = X extends Y ? Z : Y
881+
type T = X extends ?Y ? ?X : Y
882+
type F<T, X, Y> = ((t: T) => X extends Y ? X : Y) extends ((t: T) => X extends Y ? Y : X) ? X : Y
883+
type F<T, X, Y> = (t: T) => X extends Y ? X : Y extends (t: T) => X extends Y ? Y : X ? X : Y
884+
885+
---
886+
(program
887+
(type_alias_declaration (type_identifier)
888+
(conditional_type
889+
(type_identifier)
890+
(type_identifier)
891+
(type_identifier)
892+
(type_identifier)))
893+
(type_alias_declaration (type_identifier)
894+
(conditional_type
895+
(type_identifier)
896+
(flow_maybe_type (type_identifier))
897+
(flow_maybe_type (type_identifier))
898+
(type_identifier)))
899+
(type_alias_declaration
900+
(type_identifier)
901+
(type_parameters
902+
(type_parameter (type_identifier))
903+
(type_parameter (type_identifier))
904+
(type_parameter (type_identifier)))
905+
(conditional_type
906+
(parenthesized_type
907+
(function_type
908+
(formal_parameters
909+
(required_parameter
910+
(identifier)
911+
(type_annotation (type_identifier))))
912+
(conditional_type
913+
(type_identifier)
914+
(type_identifier)
915+
(type_identifier)
916+
(type_identifier))))
917+
(parenthesized_type
918+
(function_type
919+
(formal_parameters
920+
(required_parameter
921+
(identifier)
922+
(type_annotation (type_identifier))))
923+
(conditional_type
924+
(type_identifier)
925+
(type_identifier)
926+
(type_identifier)
927+
(type_identifier))))
928+
(type_identifier)
929+
(type_identifier)))
930+
(type_alias_declaration
931+
(type_identifier)
932+
(type_parameters
933+
(type_parameter (type_identifier))
934+
(type_parameter (type_identifier))
935+
(type_parameter (type_identifier)))
936+
(function_type
937+
(formal_parameters
938+
(required_parameter
939+
(identifier)
940+
(type_annotation (type_identifier))))
941+
(conditional_type
942+
(conditional_type
943+
(type_identifier)
944+
(type_identifier)
945+
(type_identifier)
946+
(type_identifier))
947+
(function_type
948+
(formal_parameters
949+
(required_parameter
950+
(identifier)
951+
(type_annotation (type_identifier))))
952+
(conditional_type
953+
(type_identifier)
954+
(type_identifier)
955+
(type_identifier)
956+
(type_identifier)))
957+
(type_identifier)
958+
(type_identifier)))))

common/define-grammar.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ const PREC = {
33
DEFINITION: 1,
44
DECLARATION: 1,
55
AS_EXPRESSION: 1,
6-
INTERSECTION: 2,
7-
UNION: 2,
6+
// INTERSECTION: 2,
7+
// UNION: 2,
8+
FUNCTION_TYPE: 1,
9+
CONDITIONAL_TYPE: 2,
10+
INTERSECTION: 3,
11+
UNION: 3,
812
PLUS: 4,
913
REL: 5,
1014
TIMES: 6,
@@ -15,10 +19,12 @@ const PREC = {
1519
NON_NULL: 10,
1620
CALL: 11,
1721
NEW: 12,
22+
FLOW_MAYBE_TYPE: 12,
1823
ARRAY_TYPE: 13,
1924
MEMBER: 14,
2025
TYPE_ASSERTION: 16,
21-
TYPE_REFERENCE: 16
26+
TYPE_REFERENCE: 16,
27+
CONSTRUCTOR_TYPE: 17
2228
};
2329

2430
module.exports = function defineGrammar(dialect) {
@@ -501,7 +507,7 @@ module.exports = function defineGrammar(dialect) {
501507
'asserts',
502508
choice(
503509
$.identifier,
504-
seq($.identifier, 'is', $._type)
510+
$.type_predicate
505511
)
506512
),
507513

@@ -528,21 +534,20 @@ module.exports = function defineGrammar(dialect) {
528534
$.labeled_tuple_type_member,
529535
),
530536

531-
constructor_type: $ => seq(
537+
constructor_type: $ => prec.left(PREC.CONSTRUCTOR_TYPE, seq(
532538
'new',
533539
optional($.type_parameters),
534540
$.formal_parameters,
535541
'=>',
536542
$._type
537-
),
543+
)),
538544

539545
_primary_type: $ => choice(
540546
$.parenthesized_type,
541547
$.predefined_type,
542548
$._type_identifier,
543549
$.nested_type_identifier,
544550
$.generic_type,
545-
$.type_predicate,
546551
$.object_type,
547552
$.array_type,
548553
$.tuple_type,
@@ -552,9 +557,20 @@ module.exports = function defineGrammar(dialect) {
552557
$.this,
553558
$.existential_type,
554559
$.literal_type,
555-
$.lookup_type
560+
$.lookup_type,
561+
$.conditional_type,
556562
),
557563

564+
conditional_type: $ => prec.left(PREC.CONDITIONAL_TYPE, seq(
565+
field('left', $._type),
566+
'extends',
567+
field('right', $._type),
568+
'?',
569+
field('consequence', $._type),
570+
':',
571+
field('alternative', $._type)
572+
)),
573+
558574
generic_type: $ => seq(
559575
choice(
560576
$._type_identifier,
@@ -569,6 +585,10 @@ module.exports = function defineGrammar(dialect) {
569585
$._type
570586
),
571587

588+
type_predicate_annotation: $ => seq(
589+
seq(':', $.type_predicate)
590+
),
591+
572592
type_query: $ => prec(PREC.TYPEOF, seq(
573593
'typeof',
574594
choice($.identifier, $.nested_identifier)
@@ -607,7 +627,7 @@ module.exports = function defineGrammar(dialect) {
607627

608628
existential_type: $ => '*',
609629

610-
flow_maybe_type: $ => prec.right(seq( '?', $._primary_type)),
630+
flow_maybe_type: $ => prec.right(PREC.FLOW_MAYBE_TYPE, seq( '?', $._primary_type)),
611631

612632
parenthesized_type: $ => seq(
613633
'(', $._type, ')'
@@ -661,7 +681,7 @@ module.exports = function defineGrammar(dialect) {
661681
field('type_parameters', optional($.type_parameters)),
662682
field('parameters', $.formal_parameters),
663683
field('return_type', optional(
664-
choice($.type_annotation, $.asserts)
684+
choice($.type_annotation, $.asserts, $.type_predicate_annotation)
665685
))
666686
),
667687

@@ -741,12 +761,12 @@ module.exports = function defineGrammar(dialect) {
741761
optional($._type), '&', $._type
742762
)),
743763

744-
function_type: $ => seq(
764+
function_type: $ => prec.left(PREC.FUNCTION_TYPE, seq(
745765
optional($.type_parameters),
746766
$.formal_parameters,
747767
'=>',
748768
$._type
749-
),
769+
)),
750770

751771
_type_identifier: $ => alias($.identifier, $.type_identifier),
752772

0 commit comments

Comments
 (0)