Skip to content

Commit 4e94719

Browse files
jack-williamsJack-Works
authored andcommitted
Minor fix for assertion predicates (microsoft#38710)
1 parent 17d8ea3 commit 4e94719

File tree

6 files changed

+235
-199
lines changed

6 files changed

+235
-199
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20474,7 +20474,7 @@ namespace ts {
2047420474
const signature = getEffectsSignature((<FlowCall>flow).node);
2047520475
if (signature) {
2047620476
const predicate = getTypePredicateOfSignature(signature);
20477-
if (predicate && predicate.kind === TypePredicateKind.AssertsIdentifier) {
20477+
if (predicate && predicate.kind === TypePredicateKind.AssertsIdentifier && !predicate.type) {
2047820478
const predicateArgument = (<FlowCall>flow).node.arguments[predicate.parameterIndex];
2047920479
if (predicateArgument && isFalseExpression(predicateArgument)) {
2048020480
return false;

tests/baselines/reference/assertionTypePredicates1.errors.txt

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(39,9): error TS7027: Unreachable code detected.
21
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(43,9): error TS7027: Unreachable code detected.
3-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(87,9): error TS7027: Unreachable code detected.
4-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(122,9): error TS7027: Unreachable code detected.
5-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(137,9): error TS7027: Unreachable code detected.
6-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(153,37): error TS1228: A type predicate is only allowed in return type position for functions and methods.
7-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(154,37): error TS1228: A type predicate is only allowed in return type position for functions and methods.
8-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(155,37): error TS1228: A type predicate is only allowed in return type position for functions and methods.
9-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(158,15): error TS1228: A type predicate is only allowed in return type position for functions and methods.
10-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(159,15): error TS1228: A type predicate is only allowed in return type position for functions and methods.
11-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(160,15): error TS1228: A type predicate is only allowed in return type position for functions and methods.
12-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(161,15): error TS1228: A type predicate is only allowed in return type position for functions and methods.
13-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(166,5): error TS2775: Assertions require every name in the call target to be declared with an explicit type annotation.
14-
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(168,5): error TS2776: Assertions require the call target to be an identifier or qualified name.
2+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(47,9): error TS7027: Unreachable code detected.
3+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(91,9): error TS7027: Unreachable code detected.
4+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(126,9): error TS7027: Unreachable code detected.
5+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(141,9): error TS7027: Unreachable code detected.
6+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(157,37): error TS1228: A type predicate is only allowed in return type position for functions and methods.
7+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(158,37): error TS1228: A type predicate is only allowed in return type position for functions and methods.
8+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(159,37): error TS1228: A type predicate is only allowed in return type position for functions and methods.
9+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(162,15): error TS1228: A type predicate is only allowed in return type position for functions and methods.
10+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(163,15): error TS1228: A type predicate is only allowed in return type position for functions and methods.
11+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(164,15): error TS1228: A type predicate is only allowed in return type position for functions and methods.
12+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(165,15): error TS1228: A type predicate is only allowed in return type position for functions and methods.
1513
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(170,5): error TS2775: Assertions require every name in the call target to be declared with an explicit type annotation.
14+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(172,5): error TS2776: Assertions require the call target to be an identifier or qualified name.
15+
tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(174,5): error TS2775: Assertions require every name in the call target to be declared with an explicit type annotation.
1616

1717

1818
==== tests/cases/conformance/controlFlow/assertionTypePredicates1.ts (15 errors) ====
@@ -46,6 +46,10 @@ tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(170,5): error TS
4646
assertIsArrayOfStrings(x);
4747
x[0].length;
4848
}
49+
if (!!true) {
50+
assertIsArrayOfStrings(false);
51+
x;
52+
}
4953
if (!!true) {
5054
assert(x === undefined || typeof x === "string");
5155
x; // string | undefined
@@ -208,7 +212,7 @@ tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(170,5): error TS
208212
assert(typeof x === "string"); // Error
209213
~~~~~~
210214
!!! error TS2775: Assertions require every name in the call target to be declared with an explicit type annotation.
211-
!!! related TS2782 tests/cases/conformance/controlFlow/assertionTypePredicates1.ts:165:11: 'assert' needs an explicit type annotation.
215+
!!! related TS2782 tests/cases/conformance/controlFlow/assertionTypePredicates1.ts:169:11: 'assert' needs an explicit type annotation.
212216
const a = [assert];
213217
a[0](typeof x === "string"); // Error
214218
~~~~
@@ -217,7 +221,7 @@ tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(170,5): error TS
217221
t1.assert(typeof x === "string"); // Error
218222
~~~~~~~~~
219223
!!! error TS2775: Assertions require every name in the call target to be declared with an explicit type annotation.
220-
!!! related TS2782 tests/cases/conformance/controlFlow/assertionTypePredicates1.ts:169:11: 't1' needs an explicit type annotation.
224+
!!! related TS2782 tests/cases/conformance/controlFlow/assertionTypePredicates1.ts:173:11: 't1' needs an explicit type annotation.
221225
const t2: Test = new Test();
222226
t2.assert(typeof x === "string");
223227
}

tests/baselines/reference/assertionTypePredicates1.js

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ function f01(x: unknown) {
2929
assertIsArrayOfStrings(x);
3030
x[0].length;
3131
}
32+
if (!!true) {
33+
assertIsArrayOfStrings(false);
34+
x;
35+
}
3236
if (!!true) {
3337
assert(x === undefined || typeof x === "string");
3438
x; // string | undefined
@@ -229,6 +233,10 @@ function f01(x) {
229233
assertIsArrayOfStrings(x);
230234
x[0].length;
231235
}
236+
if (!!true) {
237+
assertIsArrayOfStrings(false);
238+
x;
239+
}
232240
if (!!true) {
233241
assert(x === undefined || typeof x === "string");
234242
x; // string | undefined

0 commit comments

Comments
 (0)