-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Uncalled function checks don't work with negation #43097
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
Closed
Closed
Changes from all commits
Commits
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
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
69 changes: 69 additions & 0 deletions
69
tests/baselines/reference/contextualOverloadListFromArrayUnion.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,69 @@ | ||
tests/cases/compiler/three.ts(28,14): error TS2803: This condition will always return false since the function is always defined. Did you mean to call it instead? | ||
|
||
|
||
==== tests/cases/compiler/one.ts (0 errors) ==== | ||
declare const y: never[] | string[]; | ||
export const yThen = y.map(item => item.length); | ||
==== tests/cases/compiler/two.ts (0 errors) ==== | ||
declare const y: number[][] | string[]; | ||
export const yThen = y.map(item => item.length); | ||
==== tests/cases/compiler/three.ts (1 errors) ==== | ||
// #42504 | ||
interface ResizeObserverCallback { | ||
(entries: ResizeObserverEntry[], observer: ResizeObserver): void; | ||
} | ||
interface ResizeObserverCallback { // duplicate for effect | ||
(entries: ResizeObserverEntry[], observer: ResizeObserver): void; | ||
} | ||
|
||
const resizeObserver = new ResizeObserver(([entry]) => { | ||
entry | ||
}); | ||
// comment in #35501 | ||
interface Callback<T> { | ||
(error: null, result: T): unknown | ||
(error: Error, result: null): unknown | ||
} | ||
|
||
interface Task<T> { | ||
(callback: Callback<T>): unknown | ||
} | ||
|
||
export function series<T>(tasks: Task<T>[], callback: Callback<T[]>): void { | ||
let index = 0 | ||
let results: T[] = [] | ||
|
||
function next() { | ||
let task = tasks[index] | ||
if (!task) { | ||
~~~~ | ||
!!! error TS2803: This condition will always return false since the function is always defined. Did you mean to call it instead? | ||
callback(null, results) | ||
} else { | ||
task((error, result) => { | ||
if (error) { | ||
callback(error, null) | ||
} else { | ||
// must use postfix-!, since `error` and `result` don't have a | ||
// causal relationship when the overloads are combined | ||
results.push(result!) | ||
next() | ||
} | ||
}) | ||
} | ||
} | ||
next() | ||
} | ||
|
||
series([ | ||
cb => setTimeout(() => cb(null, 1), 300), | ||
cb => setTimeout(() => cb(null, 2), 200), | ||
cb => setTimeout(() => cb(null, 3), 100), | ||
], (error, results) => { | ||
if (error) { | ||
console.error(error) | ||
} else { | ||
console.log(results) | ||
} | ||
}) | ||
|
24 changes: 24 additions & 0 deletions
24
tests/baselines/reference/uncalledFunctionChecksDontWorkWithNegation.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,24 @@ | ||
tests/cases/compiler/uncalledFunctionChecksDontWorkWithNegation.ts(4,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
tests/cases/compiler/uncalledFunctionChecksDontWorkWithNegation.ts(8,6): error TS2803: This condition will always return false since the function is always defined. Did you mean to call it instead? | ||
|
||
|
||
==== tests/cases/compiler/uncalledFunctionChecksDontWorkWithNegation.ts (2 errors) ==== | ||
declare function isFoo(): boolean; | ||
declare const isUndefinedFoo: (() => boolean) | undefined; | ||
|
||
if (isFoo) { | ||
~~~~~ | ||
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? | ||
// error | ||
} | ||
|
||
if (!isFoo) { | ||
~~~~~ | ||
!!! error TS2803: This condition will always return false since the function is always defined. Did you mean to call it instead? | ||
// error | ||
} | ||
|
||
if (!isUndefinedFoo) { | ||
// no error | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
tests/baselines/reference/uncalledFunctionChecksDontWorkWithNegation.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,27 @@ | ||
//// [uncalledFunctionChecksDontWorkWithNegation.ts] | ||
declare function isFoo(): boolean; | ||
declare const isUndefinedFoo: (() => boolean) | undefined; | ||
|
||
if (isFoo) { | ||
// error | ||
} | ||
|
||
if (!isFoo) { | ||
// error | ||
} | ||
|
||
if (!isUndefinedFoo) { | ||
// no error | ||
} | ||
|
||
|
||
//// [uncalledFunctionChecksDontWorkWithNegation.js] | ||
if (isFoo) { | ||
// error | ||
} | ||
if (!isFoo) { | ||
// error | ||
} | ||
if (!isUndefinedFoo) { | ||
// no error | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/baselines/reference/uncalledFunctionChecksDontWorkWithNegation.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,25 @@ | ||
=== tests/cases/compiler/uncalledFunctionChecksDontWorkWithNegation.ts === | ||
declare function isFoo(): boolean; | ||
>isFoo : Symbol(isFoo, Decl(uncalledFunctionChecksDontWorkWithNegation.ts, 0, 0)) | ||
|
||
declare const isUndefinedFoo: (() => boolean) | undefined; | ||
>isUndefinedFoo : Symbol(isUndefinedFoo, Decl(uncalledFunctionChecksDontWorkWithNegation.ts, 1, 13)) | ||
|
||
if (isFoo) { | ||
>isFoo : Symbol(isFoo, Decl(uncalledFunctionChecksDontWorkWithNegation.ts, 0, 0)) | ||
|
||
// error | ||
} | ||
|
||
if (!isFoo) { | ||
>isFoo : Symbol(isFoo, Decl(uncalledFunctionChecksDontWorkWithNegation.ts, 0, 0)) | ||
|
||
// error | ||
} | ||
|
||
if (!isUndefinedFoo) { | ||
>isUndefinedFoo : Symbol(isUndefinedFoo, Decl(uncalledFunctionChecksDontWorkWithNegation.ts, 1, 13)) | ||
|
||
// no error | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
tests/baselines/reference/uncalledFunctionChecksDontWorkWithNegation.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,27 @@ | ||
=== tests/cases/compiler/uncalledFunctionChecksDontWorkWithNegation.ts === | ||
declare function isFoo(): boolean; | ||
>isFoo : () => boolean | ||
|
||
declare const isUndefinedFoo: (() => boolean) | undefined; | ||
>isUndefinedFoo : (() => boolean) | undefined | ||
|
||
if (isFoo) { | ||
>isFoo : () => boolean | ||
|
||
// error | ||
} | ||
|
||
if (!isFoo) { | ||
>!isFoo : false | ||
>isFoo : () => boolean | ||
|
||
// error | ||
} | ||
|
||
if (!isUndefinedFoo) { | ||
>!isUndefinedFoo : boolean | ||
>isUndefinedFoo : (() => boolean) | undefined | ||
|
||
// no error | ||
} | ||
|
16 changes: 16 additions & 0 deletions
16
tests/cases/compiler/uncalledFunctionChecksDontWorkWithNegation.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,16 @@ | ||
// @strictNullChecks: true | ||
|
||
declare function isFoo(): boolean; | ||
declare const isUndefinedFoo: (() => boolean) | undefined; | ||
|
||
if (isFoo) { | ||
// error | ||
} | ||
|
||
if (!isFoo) { | ||
// error | ||
} | ||
|
||
if (!isUndefinedFoo) { | ||
// no error | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would also like to see what happens when we issue this error for unawaited promises, like so: declare function foo(): Promise<string | undefined>;
const awaitedValue = foo();
if (awaitedValue) { // Emits ts2801
// ...
}
if (!awaitedValue) { // What happens here?
// ...
} |
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.
would be nice to have tests for when those negated expressions are part of larger logical expressions (e.g.
!isFoo || x
, etc)