Skip to content

Fix reachability analysis for 'assert x is T' assertion type predicates #39177

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20459,7 +20459,7 @@ namespace ts {
const signature = getEffectsSignature((<FlowCall>flow).node);
if (signature) {
const predicate = getTypePredicateOfSignature(signature);
if (predicate && predicate.kind === TypePredicateKind.AssertsIdentifier) {
if (predicate && predicate.kind === TypePredicateKind.AssertsIdentifier && !predicate.type) {
const predicateArgument = (<FlowCall>flow).node.arguments[predicate.parameterIndex];
if (predicateArgument && isFalseExpression(predicateArgument)) {
return false;
Expand Down
8 changes: 8 additions & 0 deletions tests/baselines/reference/assertionTypePredicates1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,12 @@ tests/cases/conformance/controlFlow/assertionTypePredicates1.ts(170,5): error TS
thing.good;
}
}

// Repro from #38699

declare function strictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;

const b = false;
strictEqual(false, b);
let b2 = b;

14 changes: 14 additions & 0 deletions tests/baselines/reference/assertionTypePredicates1.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ function example1(things: Thing[]) {
thing.good;
}
}

// Repro from #38699

declare function strictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;

const b = false;
strictEqual(false, b);
let b2 = b;


//// [assertionTypePredicates1.js]
Expand Down Expand Up @@ -374,6 +382,9 @@ function example1(things) {
thing.good;
}
}
var b = false;
strictEqual(false, b);
var b2 = b;


//// [assertionTypePredicates1.d.ts]
Expand Down Expand Up @@ -426,3 +437,6 @@ interface GoodThing {
good: true;
}
declare function example1(things: Thing[]): void;
declare function strictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;
declare const b = false;
declare let b2: boolean;
24 changes: 24 additions & 0 deletions tests/baselines/reference/assertionTypePredicates1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,27 @@ function example1(things: Thing[]) {
}
}

// Repro from #38699

declare function strictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;
>strictEqual : Symbol(strictEqual, Decl(assertionTypePredicates1.ts, 190, 1))
>T : Symbol(T, Decl(assertionTypePredicates1.ts, 194, 29))
>actual : Symbol(actual, Decl(assertionTypePredicates1.ts, 194, 32))
>expected : Symbol(expected, Decl(assertionTypePredicates1.ts, 194, 44))
>T : Symbol(T, Decl(assertionTypePredicates1.ts, 194, 29))
>message : Symbol(message, Decl(assertionTypePredicates1.ts, 194, 57))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>actual : Symbol(actual, Decl(assertionTypePredicates1.ts, 194, 32))
>T : Symbol(T, Decl(assertionTypePredicates1.ts, 194, 29))

const b = false;
>b : Symbol(b, Decl(assertionTypePredicates1.ts, 196, 5))

strictEqual(false, b);
>strictEqual : Symbol(strictEqual, Decl(assertionTypePredicates1.ts, 190, 1))
>b : Symbol(b, Decl(assertionTypePredicates1.ts, 196, 5))

let b2 = b;
>b2 : Symbol(b2, Decl(assertionTypePredicates1.ts, 198, 3))
>b : Symbol(b, Decl(assertionTypePredicates1.ts, 196, 5))

22 changes: 22 additions & 0 deletions tests/baselines/reference/assertionTypePredicates1.types
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,25 @@ function example1(things: Thing[]) {
}
}

// Repro from #38699

declare function strictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;
>strictEqual : <T>(actual: any, expected: T, message?: string | Error | undefined) => asserts actual is T
>actual : any
>expected : T
>message : string | Error | undefined

const b = false;
>b : false
>false : false

strictEqual(false, b);
>strictEqual(false, b) : void
>strictEqual : <T>(actual: any, expected: T, message?: string | Error | undefined) => asserts actual is T
>false : false
>b : false

let b2 = b;
>b2 : boolean
>b : false

Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,11 @@ function example1(things: Thing[]) {
thing.good;
}
}

// Repro from #38699

declare function strictEqual<T>(actual: any, expected: T, message?: string | Error): asserts actual is T;

const b = false;
strictEqual(false, b);
let b2 = b;