-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Add related diagnostic to "used before defined" if type is a function that returns a union with undefined #33171
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
6 commits
Select commit
Hold shift + click to select a range
f96d68c
Add "use before defined" diagnostic
OzairP 332bf6d
Make "use before defined" diagnostic as related information to TS2454
OzairP ebda836
Add baseline tests for "use before defined"
OzairP b8d8bb1
Add test for type alias union with undefined for "use before defined"…
OzairP 412cc7a
Merge master
orta 18224bb
Update baselines
orta 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
74 changes: 74 additions & 0 deletions
74
.../baselines/reference/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.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,74 @@ | ||
tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts(2,1): error TS2454: Variable 'a' is used before being assigned. | ||
tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts(6,1): error TS2454: Variable 'c' is used before being assigned. | ||
tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts(10,1): error TS2454: Variable 'd' is used before being assigned. | ||
tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts(13,1): error TS2454: Variable 'e' is used before being assigned. | ||
tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts(16,1): error TS2454: Variable 'f' is used before being assigned. | ||
tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts(19,1): error TS2454: Variable 'g' is used before being assigned. | ||
tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts(24,1): error TS2454: Variable 'h' is used before being assigned. | ||
tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts(28,1): error TS2454: Variable 'i' is used before being assigned. | ||
tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts(32,1): error TS2454: Variable 'j' is used before being assigned. | ||
tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts(36,1): error TS2454: Variable 'j' is used before being assigned. | ||
|
||
|
||
==== tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts (10 errors) ==== | ||
let a: () => void | undefined; | ||
a; // Error did you mean to paren this function type | ||
~ | ||
!!! error TS2454: Variable 'a' is used before being assigned. | ||
!!! related TS1360 tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts:1:8: Did you mean to parenthesize this function type? | ||
|
||
function b (): void | undefined {} | ||
let c: typeof b; | ||
c; | ||
~ | ||
!!! error TS2454: Variable 'c' is used before being assigned. | ||
|
||
type Undefined = undefined | ||
let d: () => void | Undefined; | ||
d; | ||
~ | ||
!!! error TS2454: Variable 'd' is used before being assigned. | ||
|
||
let e: () => string | void | number | object | undefined; | ||
e; // Error did you mean to paren... | ||
~ | ||
!!! error TS2454: Variable 'e' is used before being assigned. | ||
!!! related TS1360 tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts:12:8: Did you mean to parenthesize this function type? | ||
|
||
let f: () => void; | ||
f; | ||
~ | ||
!!! error TS2454: Variable 'f' is used before being assigned. | ||
|
||
let g: () => undefined; | ||
g; | ||
~ | ||
!!! error TS2454: Variable 'g' is used before being assigned. | ||
|
||
type T1 = undefined | string; | ||
type T2 = undefined | void; | ||
let h: () => T1 & T2; | ||
h; | ||
~ | ||
!!! error TS2454: Variable 'h' is used before being assigned. | ||
|
||
type T3 = void | undefined; | ||
let i: () => T3; | ||
i; | ||
~ | ||
!!! error TS2454: Variable 'i' is used before being assigned. | ||
|
||
type T4 = () => void | undefined; | ||
let j: T4; | ||
j; // Error did you mean to paren... | ||
~ | ||
!!! error TS2454: Variable 'j' is used before being assigned. | ||
!!! related TS1360 tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts:30:11: Did you mean to parenthesize this function type? | ||
|
||
type T5 = () => void | ||
let k: T5 | undefined | ||
j; | ||
~ | ||
!!! error TS2454: Variable 'j' is used before being assigned. | ||
!!! related TS1360 tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts:30:11: Did you mean to parenthesize this function type? | ||
|
61 changes: 61 additions & 0 deletions
61
tests/baselines/reference/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.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,61 @@ | ||
//// [functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts] | ||
let a: () => void | undefined; | ||
a; // Error did you mean to paren this function type | ||
|
||
function b (): void | undefined {} | ||
let c: typeof b; | ||
c; | ||
|
||
type Undefined = undefined | ||
let d: () => void | Undefined; | ||
d; | ||
|
||
let e: () => string | void | number | object | undefined; | ||
e; // Error did you mean to paren... | ||
|
||
let f: () => void; | ||
f; | ||
|
||
let g: () => undefined; | ||
g; | ||
|
||
type T1 = undefined | string; | ||
type T2 = undefined | void; | ||
let h: () => T1 & T2; | ||
h; | ||
|
||
type T3 = void | undefined; | ||
let i: () => T3; | ||
i; | ||
|
||
type T4 = () => void | undefined; | ||
let j: T4; | ||
j; // Error did you mean to paren... | ||
|
||
type T5 = () => void | ||
let k: T5 | undefined | ||
j; | ||
|
||
|
||
//// [functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.js] | ||
var a; | ||
a; // Error did you mean to paren this function type | ||
function b() { } | ||
var c; | ||
c; | ||
var d; | ||
d; | ||
var e; | ||
e; // Error did you mean to paren... | ||
var f; | ||
f; | ||
var g; | ||
g; | ||
var h; | ||
h; | ||
var i; | ||
i; | ||
var j; | ||
j; // Error did you mean to paren... | ||
var k; | ||
j; |
89 changes: 89 additions & 0 deletions
89
tests/baselines/reference/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.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,89 @@ | ||
=== tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts === | ||
let a: () => void | undefined; | ||
>a : Symbol(a, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 0, 3)) | ||
|
||
a; // Error did you mean to paren this function type | ||
>a : Symbol(a, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 0, 3)) | ||
|
||
function b (): void | undefined {} | ||
>b : Symbol(b, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 1, 2)) | ||
|
||
let c: typeof b; | ||
>c : Symbol(c, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 4, 3)) | ||
>b : Symbol(b, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 1, 2)) | ||
|
||
c; | ||
>c : Symbol(c, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 4, 3)) | ||
|
||
type Undefined = undefined | ||
>Undefined : Symbol(Undefined, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 5, 2)) | ||
|
||
let d: () => void | Undefined; | ||
>d : Symbol(d, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 8, 3)) | ||
>Undefined : Symbol(Undefined, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 5, 2)) | ||
|
||
d; | ||
>d : Symbol(d, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 8, 3)) | ||
|
||
let e: () => string | void | number | object | undefined; | ||
>e : Symbol(e, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 11, 3)) | ||
|
||
e; // Error did you mean to paren... | ||
>e : Symbol(e, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 11, 3)) | ||
|
||
let f: () => void; | ||
>f : Symbol(f, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 14, 3)) | ||
|
||
f; | ||
>f : Symbol(f, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 14, 3)) | ||
|
||
let g: () => undefined; | ||
>g : Symbol(g, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 17, 3)) | ||
|
||
g; | ||
>g : Symbol(g, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 17, 3)) | ||
|
||
type T1 = undefined | string; | ||
>T1 : Symbol(T1, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 18, 2)) | ||
|
||
type T2 = undefined | void; | ||
>T2 : Symbol(T2, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 20, 29)) | ||
|
||
let h: () => T1 & T2; | ||
>h : Symbol(h, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 22, 3)) | ||
>T1 : Symbol(T1, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 18, 2)) | ||
>T2 : Symbol(T2, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 20, 29)) | ||
|
||
h; | ||
>h : Symbol(h, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 22, 3)) | ||
|
||
type T3 = void | undefined; | ||
>T3 : Symbol(T3, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 23, 2)) | ||
|
||
let i: () => T3; | ||
>i : Symbol(i, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 26, 3)) | ||
>T3 : Symbol(T3, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 23, 2)) | ||
|
||
i; | ||
>i : Symbol(i, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 26, 3)) | ||
|
||
type T4 = () => void | undefined; | ||
>T4 : Symbol(T4, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 27, 2)) | ||
|
||
let j: T4; | ||
>j : Symbol(j, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 30, 3)) | ||
>T4 : Symbol(T4, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 27, 2)) | ||
|
||
j; // Error did you mean to paren... | ||
>j : Symbol(j, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 30, 3)) | ||
|
||
type T5 = () => void | ||
>T5 : Symbol(T5, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 31, 2)) | ||
|
||
let k: T5 | undefined | ||
>k : Symbol(k, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 34, 3)) | ||
>T5 : Symbol(T5, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 31, 2)) | ||
|
||
j; | ||
>j : Symbol(j, Decl(functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts, 30, 3)) | ||
|
83 changes: 83 additions & 0 deletions
83
tests/baselines/reference/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.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,83 @@ | ||
=== tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts === | ||
let a: () => void | undefined; | ||
>a : () => void | undefined | ||
|
||
a; // Error did you mean to paren this function type | ||
>a : () => void | undefined | ||
|
||
function b (): void | undefined {} | ||
>b : () => void | undefined | ||
|
||
let c: typeof b; | ||
>c : () => void | undefined | ||
>b : () => void | undefined | ||
|
||
c; | ||
>c : () => void | undefined | ||
|
||
type Undefined = undefined | ||
>Undefined : undefined | ||
|
||
let d: () => void | Undefined; | ||
>d : () => void | undefined | ||
|
||
d; | ||
>d : () => void | undefined | ||
|
||
let e: () => string | void | number | object | undefined; | ||
>e : () => string | number | void | object | undefined | ||
|
||
e; // Error did you mean to paren... | ||
>e : () => string | number | void | object | undefined | ||
|
||
let f: () => void; | ||
>f : () => void | ||
|
||
f; | ||
>f : () => void | ||
|
||
let g: () => undefined; | ||
>g : () => undefined | ||
|
||
g; | ||
>g : () => undefined | ||
|
||
type T1 = undefined | string; | ||
>T1 : T1 | ||
|
||
type T2 = undefined | void; | ||
>T2 : void | undefined | ||
|
||
let h: () => T1 & T2; | ||
>h : () => undefined | ||
|
||
h; | ||
>h : () => undefined | ||
|
||
type T3 = void | undefined; | ||
>T3 : void | undefined | ||
|
||
let i: () => T3; | ||
>i : () => void | undefined | ||
|
||
i; | ||
>i : () => void | undefined | ||
|
||
type T4 = () => void | undefined; | ||
>T4 : T4 | ||
|
||
let j: T4; | ||
>j : T4 | ||
|
||
j; // Error did you mean to paren... | ||
>j : T4 | ||
|
||
type T5 = () => void | ||
>T5 : T5 | ||
|
||
let k: T5 | undefined | ||
>k : T5 | undefined | ||
|
||
j; | ||
>j : T4 | ||
|
38 changes: 38 additions & 0 deletions
38
tests/cases/compiler/functionTypeReturnsUnionWithUndefinedWithStrictNullChecks.ts
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,38 @@ | ||
// @strictNullChecks: true | ||
|
||
let a: () => void | undefined; | ||
a; // Error did you mean to paren this function type | ||
|
||
function b (): void | undefined {} | ||
let c: typeof b; | ||
c; | ||
|
||
type Undefined = undefined | ||
let d: () => void | Undefined; | ||
d; | ||
|
||
let e: () => string | void | number | object | undefined; | ||
e; // Error did you mean to paren... | ||
|
||
let f: () => void; | ||
f; | ||
|
||
let g: () => undefined; | ||
g; | ||
|
||
type T1 = undefined | string; | ||
type T2 = undefined | void; | ||
let h: () => T1 & T2; | ||
h; | ||
|
||
type T3 = void | undefined; | ||
let i: () => T3; | ||
i; | ||
|
||
type T4 = () => void | undefined; | ||
let j: T4; | ||
j; // Error did you mean to paren... | ||
|
||
type T5 = () => void | ||
let k: T5 | undefined | ||
j; |
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.
Uh oh!
There was an error while loading. Please reload this page.