@@ -3570,15 +3570,31 @@ namespace ts {
3570
3570
}
3571
3571
3572
3572
function parseFunctionOrConstructorTypeToError (
3573
- functionTypeDiagnostic : DiagnosticMessage ,
3574
- constructorTypeDiagnostic : DiagnosticMessage
3573
+ isInUnionType : boolean
3575
3574
) : TypeNode | undefined {
3576
3575
// the function type and constructor type shorthand notation
3577
3576
// are not allowed directly in unions and intersections, but we'll
3578
3577
// try to parse them gracefully and issue a helpful message.
3579
3578
if ( isStartOfFunctionType ( ) || token ( ) === SyntaxKind . NewKeyword ) {
3580
3579
const type = parseFunctionOrConstructorType ( ) ;
3581
- parseErrorAtRange ( type , isFunctionTypeNode ( type ) ? functionTypeDiagnostic : constructorTypeDiagnostic ) ;
3580
+ let diagnostic : DiagnosticMessage ;
3581
+ if ( isFunctionTypeNode ( type ) ) {
3582
+ if ( isInUnionType ) {
3583
+ diagnostic = Diagnostics . Function_type_notation_must_be_parenthesized_when_used_in_a_union_type ;
3584
+ }
3585
+ else {
3586
+ diagnostic = Diagnostics . Function_type_notation_must_be_parenthesized_when_used_in_an_intersection_type ;
3587
+ }
3588
+ }
3589
+ else {
3590
+ if ( isInUnionType ) {
3591
+ diagnostic = Diagnostics . Constructor_type_notation_must_be_parenthesized_when_used_in_a_union_type ;
3592
+ }
3593
+ else {
3594
+ diagnostic = Diagnostics . Constructor_type_notation_must_be_parenthesized_when_used_in_an_intersection_type ;
3595
+ }
3596
+ }
3597
+ parseErrorAtRange ( type , diagnostic ) ;
3582
3598
return type ;
3583
3599
}
3584
3600
return undefined ;
@@ -3587,43 +3603,29 @@ namespace ts {
3587
3603
function parseUnionOrIntersectionType (
3588
3604
operator : SyntaxKind . BarToken | SyntaxKind . AmpersandToken ,
3589
3605
parseConstituentType : ( ) => TypeNode ,
3590
- createTypeNode : ( types : NodeArray < TypeNode > ) => UnionOrIntersectionTypeNode ,
3591
- functionTypeDiagnostic : DiagnosticMessage ,
3592
- constructorTypeDiagnostic : DiagnosticMessage
3606
+ createTypeNode : ( types : NodeArray < TypeNode > ) => UnionOrIntersectionTypeNode
3593
3607
) : TypeNode {
3594
3608
const pos = getNodePos ( ) ;
3595
3609
const hasLeadingOperator = parseOptional ( operator ) ;
3596
3610
let type = hasLeadingOperator ?
3597
- parseFunctionOrConstructorTypeToError ( functionTypeDiagnostic , constructorTypeDiagnostic ) || parseConstituentType ( ) :
3611
+ parseFunctionOrConstructorTypeToError ( operator === SyntaxKind . BarToken ) || parseConstituentType ( ) :
3598
3612
parseConstituentType ( ) ;
3599
3613
if ( token ( ) === operator || hasLeadingOperator ) {
3600
3614
const types = [ type ] ;
3601
3615
while ( parseOptional ( operator ) ) {
3602
- types . push ( parseFunctionOrConstructorTypeToError ( functionTypeDiagnostic , constructorTypeDiagnostic ) || parseConstituentType ( ) ) ;
3616
+ types . push ( parseFunctionOrConstructorTypeToError ( operator === SyntaxKind . BarToken ) || parseConstituentType ( ) ) ;
3603
3617
}
3604
3618
type = finishNode ( createTypeNode ( createNodeArray ( types , pos ) ) , pos ) ;
3605
3619
}
3606
3620
return type ;
3607
3621
}
3608
3622
3609
3623
function parseIntersectionTypeOrHigher ( ) : TypeNode {
3610
- return parseUnionOrIntersectionType (
3611
- SyntaxKind . AmpersandToken ,
3612
- parseTypeOperatorOrHigher ,
3613
- factory . createIntersectionTypeNode ,
3614
- Diagnostics . Function_type_notation_must_be_parenthesized_when_used_in_an_intersection_type ,
3615
- Diagnostics . Constructor_type_notation_must_be_parenthesized_when_used_in_an_intersection_type
3616
- ) ;
3624
+ return parseUnionOrIntersectionType ( SyntaxKind . AmpersandToken , parseTypeOperatorOrHigher , factory . createIntersectionTypeNode ) ;
3617
3625
}
3618
3626
3619
3627
function parseUnionTypeOrHigher ( ) : TypeNode {
3620
- return parseUnionOrIntersectionType (
3621
- SyntaxKind . BarToken ,
3622
- parseIntersectionTypeOrHigher ,
3623
- factory . createUnionTypeNode ,
3624
- Diagnostics . Function_type_notation_must_be_parenthesized_when_used_in_a_union_type ,
3625
- Diagnostics . Constructor_type_notation_must_be_parenthesized_when_used_in_a_union_type
3626
- ) ;
3628
+ return parseUnionOrIntersectionType ( SyntaxKind . BarToken , parseIntersectionTypeOrHigher , factory . createUnionTypeNode ) ;
3627
3629
}
3628
3630
3629
3631
function isStartOfFunctionType ( ) : boolean {
0 commit comments