-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Better error message for unparenthesized function/constructor type notation in union/intersection types #39570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
eb231ba
add graceful error message for unparenthesized function types in unio…
uhyo 6774f90
add unparenthesizedFunctionTypeInUnionOrIntersection test
uhyo 50ab58c
add unparenthesizedConstructorTypeInUnionOrIntersection test
uhyo e1c2a51
Update src/compiler/parser.ts
uhyo 4193b78
pass isInUnionType to parseFunctionOrConstructorTypeToError
uhyo 6bda0cc
Apply suggestions from code review
uhyo 38167bc
syntax fix
uhyo 32562ff
refactor isStartOfFunctionType into isStartOfFunctionTypeOrConstructo…
uhyo 98b9d82
Update src/compiler/parser.ts
uhyo e4c84fc
hoist isUnionType
uhyo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
tests/baselines/reference/unparenthesizedConstructorTypeInUnionOrIntersection.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(1,19): error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(2,19): error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(3,12): error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(4,12): error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(5,19): error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(8,4): error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(11,19): error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(12,19): error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(13,12): error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(14,12): error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(15,19): error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(18,4): error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(20,37): error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(21,31): error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(22,16): error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts(22,41): error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
|
||
|
||
==== tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts (16 errors) ==== | ||
type U1 = string | new () => void; | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
type U2 = string | new (foo: number) => void | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
type U3 = | new () => number | ||
~~~~~~~~~~~~~~~~~ | ||
!!! error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
type U4 = | new (foo?: number) => void; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
type U5 = string | new (number: number, foo?: string) => void | number; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
type U6 = | ||
| string | ||
| new (...args: any[]) => void | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| number; | ||
~~~~~~~~~~ | ||
!!! error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
|
||
type I1 = string & new () => void; | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
type I2 = string & new (...foo: number[]) => void; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
type I3 = & new () => boolean | ||
~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
type I4 = & new () => boolean & null; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
type I5 = string & new (any: any, any2: any) => any & any; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
type I6 = | ||
& string | ||
& new (foo: any) => void; | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
|
||
type M1 = string | number & string | new () => number; | ||
~~~~~~~~~~~~~~~~~ | ||
!!! error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
type M2 = any & string | any & new () => void; | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
type M3 = any & new (foo: any) => void | new () => void & any; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1388: Constructor type notation must be parenthesized when used in an intersection type. | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1386: Constructor type notation must be parenthesized when used in a union type. | ||
|
||
type OK1 = string | (new ()=> void); | ||
type OK2 = string | (new ()=> string | number); | ||
|
29 changes: 29 additions & 0 deletions
29
tests/baselines/reference/unparenthesizedConstructorTypeInUnionOrIntersection.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//// [unparenthesizedConstructorTypeInUnionOrIntersection.ts] | ||
type U1 = string | new () => void; | ||
type U2 = string | new (foo: number) => void | ||
type U3 = | new () => number | ||
type U4 = | new (foo?: number) => void; | ||
type U5 = string | new (number: number, foo?: string) => void | number; | ||
type U6 = | ||
| string | ||
| new (...args: any[]) => void | ||
| number; | ||
|
||
type I1 = string & new () => void; | ||
type I2 = string & new (...foo: number[]) => void; | ||
type I3 = & new () => boolean | ||
type I4 = & new () => boolean & null; | ||
type I5 = string & new (any: any, any2: any) => any & any; | ||
type I6 = | ||
& string | ||
& new (foo: any) => void; | ||
|
||
type M1 = string | number & string | new () => number; | ||
type M2 = any & string | any & new () => void; | ||
type M3 = any & new (foo: any) => void | new () => void & any; | ||
|
||
type OK1 = string | (new ()=> void); | ||
type OK2 = string | (new ()=> string | number); | ||
|
||
|
||
//// [unparenthesizedConstructorTypeInUnionOrIntersection.js] |
70 changes: 70 additions & 0 deletions
70
tests/baselines/reference/unparenthesizedConstructorTypeInUnionOrIntersection.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
=== tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts === | ||
type U1 = string | new () => void; | ||
>U1 : Symbol(U1, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 0, 0)) | ||
|
||
type U2 = string | new (foo: number) => void | ||
>U2 : Symbol(U2, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 0, 34)) | ||
>foo : Symbol(foo, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 1, 24)) | ||
|
||
type U3 = | new () => number | ||
>U3 : Symbol(U3, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 1, 44)) | ||
|
||
type U4 = | new (foo?: number) => void; | ||
>U4 : Symbol(U4, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 2, 28)) | ||
>foo : Symbol(foo, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 3, 17)) | ||
|
||
type U5 = string | new (number: number, foo?: string) => void | number; | ||
>U5 : Symbol(U5, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 3, 39)) | ||
>number : Symbol(number, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 4, 24)) | ||
>foo : Symbol(foo, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 4, 39)) | ||
|
||
type U6 = | ||
>U6 : Symbol(U6, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 4, 71)) | ||
|
||
| string | ||
| new (...args: any[]) => void | ||
>args : Symbol(args, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 7, 9)) | ||
|
||
| number; | ||
|
||
type I1 = string & new () => void; | ||
>I1 : Symbol(I1, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 8, 11)) | ||
|
||
type I2 = string & new (...foo: number[]) => void; | ||
>I2 : Symbol(I2, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 10, 34)) | ||
>foo : Symbol(foo, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 11, 24)) | ||
|
||
type I3 = & new () => boolean | ||
>I3 : Symbol(I3, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 11, 50)) | ||
|
||
type I4 = & new () => boolean & null; | ||
>I4 : Symbol(I4, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 12, 29)) | ||
|
||
type I5 = string & new (any: any, any2: any) => any & any; | ||
>I5 : Symbol(I5, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 13, 37)) | ||
>any : Symbol(any, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 14, 24)) | ||
>any2 : Symbol(any2, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 14, 33)) | ||
|
||
type I6 = | ||
>I6 : Symbol(I6, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 14, 58)) | ||
|
||
& string | ||
& new (foo: any) => void; | ||
>foo : Symbol(foo, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 17, 9)) | ||
|
||
type M1 = string | number & string | new () => number; | ||
>M1 : Symbol(M1, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 17, 27)) | ||
|
||
type M2 = any & string | any & new () => void; | ||
>M2 : Symbol(M2, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 19, 54)) | ||
|
||
type M3 = any & new (foo: any) => void | new () => void & any; | ||
>M3 : Symbol(M3, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 20, 46)) | ||
>foo : Symbol(foo, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 21, 21)) | ||
|
||
type OK1 = string | (new ()=> void); | ||
>OK1 : Symbol(OK1, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 21, 62)) | ||
|
||
type OK2 = string | (new ()=> string | number); | ||
>OK2 : Symbol(OK2, Decl(unparenthesizedConstructorTypeInUnionOrIntersection.ts, 23, 36)) | ||
|
71 changes: 71 additions & 0 deletions
71
tests/baselines/reference/unparenthesizedConstructorTypeInUnionOrIntersection.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
=== tests/cases/compiler/unparenthesizedConstructorTypeInUnionOrIntersection.ts === | ||
type U1 = string | new () => void; | ||
>U1 : U1 | ||
|
||
type U2 = string | new (foo: number) => void | ||
>U2 : U2 | ||
>foo : number | ||
|
||
type U3 = | new () => number | ||
>U3 : new () => number | ||
|
||
type U4 = | new (foo?: number) => void; | ||
>U4 : new (foo?: number) => void | ||
>foo : number | ||
|
||
type U5 = string | new (number: number, foo?: string) => void | number; | ||
>U5 : U5 | ||
>number : number | ||
>foo : string | ||
|
||
type U6 = | ||
>U6 : U6 | ||
|
||
| string | ||
| new (...args: any[]) => void | ||
>args : any[] | ||
|
||
| number; | ||
|
||
type I1 = string & new () => void; | ||
>I1 : I1 | ||
|
||
type I2 = string & new (...foo: number[]) => void; | ||
>I2 : I2 | ||
>foo : number[] | ||
|
||
type I3 = & new () => boolean | ||
>I3 : new () => boolean | ||
|
||
type I4 = & new () => boolean & null; | ||
>I4 : new () => boolean & null | ||
>null : null | ||
|
||
type I5 = string & new (any: any, any2: any) => any & any; | ||
>I5 : I5 | ||
>any : any | ||
>any2 : any | ||
|
||
type I6 = | ||
>I6 : I6 | ||
|
||
& string | ||
& new (foo: any) => void; | ||
>foo : any | ||
|
||
type M1 = string | number & string | new () => number; | ||
>M1 : M1 | ||
|
||
type M2 = any & string | any & new () => void; | ||
>M2 : any | ||
|
||
type M3 = any & new (foo: any) => void | new () => void & any; | ||
>M3 : any | ||
>foo : any | ||
|
||
type OK1 = string | (new ()=> void); | ||
>OK1 : OK1 | ||
|
||
type OK2 = string | (new ()=> string | number); | ||
>OK2 : OK2 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
side note: I bet this newish function could replace a lot of other lower-level error-reporting functions in the parser; it's only used 3 other places right now.