Skip to content

Commit a3d207a

Browse files
Improve (and actually use) "always truthy promise" error (#43023)
* Modify error message and actually use the error message. * Accepted baselines.
1 parent 11097c6 commit a3d207a

8 files changed

+57
-56
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35248,8 +35248,8 @@ namespace ts {
3524835248
errorAndMaybeSuggestAwait(
3524935249
condExpr,
3525035250
/*maybeMissingAwait*/ true,
35251-
Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap,
35252-
"true", getTypeNameForErrorDisplay(type), "false");
35251+
Diagnostics.This_condition_will_always_return_true_since_this_0_appears_to_always_be_defined,
35252+
getTypeNameForErrorDisplay(type));
3525335253
return;
3525435254
}
3525535255

@@ -35282,7 +35282,7 @@ namespace ts {
3528235282
const isUsed = isBinaryExpression(condExpr.parent) && isFunctionUsedInBinaryExpressionChain(condExpr.parent, testedSymbol)
3528335283
|| body && isFunctionUsedInConditionBody(condExpr, body, testedNode, testedSymbol);
3528435284
if (!isUsed) {
35285-
error(location, Diagnostics.This_condition_will_always_return_true_since_the_function_is_always_defined_Did_you_mean_to_call_it_instead);
35285+
error(location, Diagnostics.This_condition_will_always_return_true_since_this_function_appears_to_always_be_defined_Did_you_mean_to_call_it_instead);
3528635286
}
3528735287
}
3528835288

src/compiler/diagnosticMessages.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3168,7 +3168,7 @@
31683168
"category": "Error",
31693169
"code": 2773
31703170
},
3171-
"This condition will always return true since the function is always defined. Did you mean to call it instead?": {
3171+
"This condition will always return true since this function appears to always be defined. Did you mean to call it instead?": {
31723172
"category": "Error",
31733173
"code": 2774
31743174
},
@@ -3276,7 +3276,7 @@
32763276
"category": "Error",
32773277
"code": 2800
32783278
},
3279-
"This condition will always return true since the Promise is always truthy.": {
3279+
"This condition will always return true since this '{0}' appears to always be defined.": {
32803280
"category": "Error",
32813281
"code": 2801
32823282
},

src/services/codefixes/addMissingAwait.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace ts.codefix {
1414
Diagnostics.Operator_0_cannot_be_applied_to_type_1.code,
1515
Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2.code,
1616
Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code,
17+
Diagnostics.This_condition_will_always_return_true_since_this_0_appears_to_always_be_defined.code,
1718
Diagnostics.Type_0_is_not_an_array_type.code,
1819
Diagnostics.Type_0_is_not_an_array_type_or_a_string_type.code,
1920
Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators.code,

src/services/codefixes/fixMissingCallParentheses.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace ts.codefix {
33
const fixId = "fixMissingCallParentheses";
44
const errorCodes = [
5-
Diagnostics.This_condition_will_always_return_true_since_the_function_is_always_defined_Did_you_mean_to_call_it_instead.code,
5+
Diagnostics.This_condition_will_always_return_true_since_this_function_appears_to_always_be_defined_Did_you_mean_to_call_it_instead.code,
66
];
77

88
registerCodeFix({

tests/baselines/reference/truthinessCallExpressionCoercion.errors.txt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
tests/cases/compiler/truthinessCallExpressionCoercion.ts(2,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
2-
tests/cases/compiler/truthinessCallExpressionCoercion.ts(18,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
3-
tests/cases/compiler/truthinessCallExpressionCoercion.ts(36,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
4-
tests/cases/compiler/truthinessCallExpressionCoercion.ts(50,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
5-
tests/cases/compiler/truthinessCallExpressionCoercion.ts(66,13): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
6-
tests/cases/compiler/truthinessCallExpressionCoercion.ts(76,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
7-
tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
1+
tests/cases/compiler/truthinessCallExpressionCoercion.ts(2,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
2+
tests/cases/compiler/truthinessCallExpressionCoercion.ts(18,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
3+
tests/cases/compiler/truthinessCallExpressionCoercion.ts(36,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
4+
tests/cases/compiler/truthinessCallExpressionCoercion.ts(50,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
5+
tests/cases/compiler/truthinessCallExpressionCoercion.ts(66,13): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
6+
tests/cases/compiler/truthinessCallExpressionCoercion.ts(76,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
7+
tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
88

99

1010
==== tests/cases/compiler/truthinessCallExpressionCoercion.ts (7 errors) ====
1111
function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) {
1212
if (required) { // error
1313
~~~~~~~~
14-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
14+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
1515
}
1616

1717
if (optional) { // ok
@@ -29,7 +29,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: Th
2929

3030
if (test) { // error
3131
~~~~
32-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
32+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
3333
console.log('test');
3434
}
3535

@@ -49,7 +49,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: Th
4949

5050
if (test) { // error
5151
~~~~
52-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
52+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
5353
[() => null].forEach(test => {
5454
test();
5555
});
@@ -65,7 +65,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: Th
6565

6666
if (x.foo.bar) { // error
6767
~~~~~~~~~
68-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
68+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
6969
}
7070

7171
if (x.foo.bar) { // ok
@@ -83,7 +83,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: Th
8383
test() {
8484
if (this.isUser) { // error
8585
~~~~~~~~~~~
86-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
86+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
8787
}
8888

8989
if (this.maybeIsUser) { // ok
@@ -95,15 +95,15 @@ tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: Th
9595
function A(stats: StatsBase<any>) {
9696
if (stats.isDirectory) { // err
9797
~~~~~~~~~~~~~~~~~
98-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
98+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
9999
console.log(`[Directory] ${stats.ctime}`)
100100
}
101101
}
102102

103103
function B(a: Nested, b: Nested) {
104104
if (a.stats.isDirectory) { // err
105105
~~~~~~~~~~~~~~~~~~~
106-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
106+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
107107
b.stats.isDirectory();
108108
}
109109
if (a.stats.isDirectory) { // ok

tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(3,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
2-
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(19,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
3-
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(33,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
4-
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(46,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
5-
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
1+
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(3,5): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
2+
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(19,5): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
3+
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(33,5): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
4+
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(46,5): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
5+
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
66

77

88
==== tests/cases/compiler/truthinessCallExpressionCoercion1.ts (5 errors) ====
99
function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) {
1010
// error
1111
required ? console.log('required') : undefined;
1212
~~~~~~~~
13-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
13+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
1414

1515
// ok
1616
optional ? console.log('optional') : undefined;
@@ -28,7 +28,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: T
2828
// error
2929
test ? console.log('test') : undefined;
3030
~~~~
31-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
31+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
3232

3333
// ok
3434
test ? console.log(test) : undefined;
@@ -44,7 +44,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: T
4444
// error
4545
test
4646
~~~~
47-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
47+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
4848
? [() => null].forEach(test => { test() })
4949
: undefined;
5050
}
@@ -59,7 +59,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: T
5959
// error
6060
x.foo.bar ? console.log('x.foo.bar') : undefined;
6161
~~~~~~~~~
62-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
62+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
6363

6464
// ok
6565
x.foo.bar ? x.foo.bar : undefined;
@@ -91,7 +91,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: T
9191
// error
9292
this.isUser ? console.log('this.isUser') : undefined;
9393
~~~~~~~~~~~
94-
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
94+
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
9595

9696
// ok
9797
this.maybeIsUser ? console.log('this.maybeIsUser') : undefined;

0 commit comments

Comments
 (0)