Skip to content

Commit 12c2323

Browse files
Fix of #58996 (#59155)
Co-authored-by: Jake Bailey <[email protected]>
1 parent 5695986 commit 12c2323

File tree

6 files changed

+220
-1
lines changed

6 files changed

+220
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38754,7 +38754,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3875438754
// "x is T" means that x is T if and only if it returns true. If it returns false then x is not T.
3875538755
// This means that if the function is called with an argument of type trueType, there can't be anything left in the `else` branch. It must reduce to `never`.
3875638756
const falseCondition = createFlowNode(FlowFlags.FalseCondition, expr, antecedent);
38757-
const falseSubtype = getFlowTypeOfReference(param.name, initType, trueType, func, falseCondition);
38757+
const falseSubtype = getReducedType(getFlowTypeOfReference(param.name, initType, trueType, func, falseCondition));
3875838758
return falseSubtype.flags & TypeFlags.Never ? trueType : undefined;
3875938759
}
3876038760

tests/baselines/reference/inferTypePredicates.errors.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,27 @@ inferTypePredicates.ts(205,7): error TS2741: Property 'z' is missing in type 'C1
343343
const rv = x === "";
344344
return rv satisfies boolean;
345345
}
346+
347+
// https://github.com/microsoft/TypeScript/issues/58996
348+
type Animal = {
349+
breath: true,
350+
};
351+
352+
type Rock = {
353+
breath: false,
354+
};
355+
356+
type Something = Animal | Rock;
357+
358+
function isAnimal(something: Something): something is Animal {
359+
return something.breath
360+
}
361+
362+
function positive(t: Something) {
363+
return isAnimal(t)
364+
}
365+
366+
function negative(t: Something) {
367+
return !isAnimal(t)
368+
}
346369

tests/baselines/reference/inferTypePredicates.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,29 @@ function isEmptyString(x: unknown) {
289289
const rv = x === "";
290290
return rv satisfies boolean;
291291
}
292+
293+
// https://github.com/microsoft/TypeScript/issues/58996
294+
type Animal = {
295+
breath: true,
296+
};
297+
298+
type Rock = {
299+
breath: false,
300+
};
301+
302+
type Something = Animal | Rock;
303+
304+
function isAnimal(something: Something): something is Animal {
305+
return something.breath
306+
}
307+
308+
function positive(t: Something) {
309+
return isAnimal(t)
310+
}
311+
312+
function negative(t: Something) {
313+
return !isAnimal(t)
314+
}
292315

293316

294317
//// [inferTypePredicates.js]
@@ -554,6 +577,15 @@ function isEmptyString(x) {
554577
var rv = x === "";
555578
return rv;
556579
}
580+
function isAnimal(something) {
581+
return something.breath;
582+
}
583+
function positive(t) {
584+
return isAnimal(t);
585+
}
586+
function negative(t) {
587+
return !isAnimal(t);
588+
}
557589

558590

559591
//// [inferTypePredicates.d.ts]
@@ -648,3 +680,13 @@ declare const foobarPred: (fb: typeof foobar) => fb is {
648680
};
649681
declare const arrTest: Array<number>;
650682
declare function isEmptyString(x: unknown): x is "";
683+
type Animal = {
684+
breath: true;
685+
};
686+
type Rock = {
687+
breath: false;
688+
};
689+
type Something = Animal | Rock;
690+
declare function isAnimal(something: Something): something is Animal;
691+
declare function positive(t: Something): t is Animal;
692+
declare function negative(t: Something): t is Rock;

tests/baselines/reference/inferTypePredicates.symbols

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,3 +802,58 @@ function isEmptyString(x: unknown) {
802802
>rv : Symbol(rv, Decl(inferTypePredicates.ts, 285, 7))
803803
}
804804

805+
// https://github.com/microsoft/TypeScript/issues/58996
806+
type Animal = {
807+
>Animal : Symbol(Animal, Decl(inferTypePredicates.ts, 287, 1))
808+
809+
breath: true,
810+
>breath : Symbol(breath, Decl(inferTypePredicates.ts, 290, 15))
811+
812+
};
813+
814+
type Rock = {
815+
>Rock : Symbol(Rock, Decl(inferTypePredicates.ts, 292, 2))
816+
817+
breath: false,
818+
>breath : Symbol(breath, Decl(inferTypePredicates.ts, 294, 13))
819+
820+
};
821+
822+
type Something = Animal | Rock;
823+
>Something : Symbol(Something, Decl(inferTypePredicates.ts, 296, 2))
824+
>Animal : Symbol(Animal, Decl(inferTypePredicates.ts, 287, 1))
825+
>Rock : Symbol(Rock, Decl(inferTypePredicates.ts, 292, 2))
826+
827+
function isAnimal(something: Something): something is Animal {
828+
>isAnimal : Symbol(isAnimal, Decl(inferTypePredicates.ts, 298, 31))
829+
>something : Symbol(something, Decl(inferTypePredicates.ts, 300, 18))
830+
>Something : Symbol(Something, Decl(inferTypePredicates.ts, 296, 2))
831+
>something : Symbol(something, Decl(inferTypePredicates.ts, 300, 18))
832+
>Animal : Symbol(Animal, Decl(inferTypePredicates.ts, 287, 1))
833+
834+
return something.breath
835+
>something.breath : Symbol(breath, Decl(inferTypePredicates.ts, 290, 15), Decl(inferTypePredicates.ts, 294, 13))
836+
>something : Symbol(something, Decl(inferTypePredicates.ts, 300, 18))
837+
>breath : Symbol(breath, Decl(inferTypePredicates.ts, 290, 15), Decl(inferTypePredicates.ts, 294, 13))
838+
}
839+
840+
function positive(t: Something) {
841+
>positive : Symbol(positive, Decl(inferTypePredicates.ts, 302, 1))
842+
>t : Symbol(t, Decl(inferTypePredicates.ts, 304, 18))
843+
>Something : Symbol(Something, Decl(inferTypePredicates.ts, 296, 2))
844+
845+
return isAnimal(t)
846+
>isAnimal : Symbol(isAnimal, Decl(inferTypePredicates.ts, 298, 31))
847+
>t : Symbol(t, Decl(inferTypePredicates.ts, 304, 18))
848+
}
849+
850+
function negative(t: Something) {
851+
>negative : Symbol(negative, Decl(inferTypePredicates.ts, 306, 1))
852+
>t : Symbol(t, Decl(inferTypePredicates.ts, 308, 18))
853+
>Something : Symbol(Something, Decl(inferTypePredicates.ts, 296, 2))
854+
855+
return !isAnimal(t)
856+
>isAnimal : Symbol(isAnimal, Decl(inferTypePredicates.ts, 298, 31))
857+
>t : Symbol(t, Decl(inferTypePredicates.ts, 308, 18))
858+
}
859+

tests/baselines/reference/inferTypePredicates.types

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,3 +1707,79 @@ function isEmptyString(x: unknown) {
17071707
> : ^^^^^^^
17081708
}
17091709

1710+
// https://github.com/microsoft/TypeScript/issues/58996
1711+
type Animal = {
1712+
>Animal : Animal
1713+
> : ^^^^^^
1714+
1715+
breath: true,
1716+
>breath : true
1717+
> : ^^^^
1718+
>true : true
1719+
> : ^^^^
1720+
1721+
};
1722+
1723+
type Rock = {
1724+
>Rock : Rock
1725+
> : ^^^^
1726+
1727+
breath: false,
1728+
>breath : false
1729+
> : ^^^^^
1730+
>false : false
1731+
> : ^^^^^
1732+
1733+
};
1734+
1735+
type Something = Animal | Rock;
1736+
>Something : Something
1737+
> : ^^^^^^^^^
1738+
1739+
function isAnimal(something: Something): something is Animal {
1740+
>isAnimal : (something: Something) => something is Animal
1741+
> : ^ ^^ ^^^^^
1742+
>something : Something
1743+
> : ^^^^^^^^^
1744+
1745+
return something.breath
1746+
>something.breath : boolean
1747+
> : ^^^^^^^
1748+
>something : Something
1749+
> : ^^^^^^^^^
1750+
>breath : boolean
1751+
> : ^^^^^^^
1752+
}
1753+
1754+
function positive(t: Something) {
1755+
>positive : (t: Something) => t is Animal
1756+
> : ^ ^^ ^^^^^^^^^^^^^^^^
1757+
>t : Something
1758+
> : ^^^^^^^^^
1759+
1760+
return isAnimal(t)
1761+
>isAnimal(t) : boolean
1762+
> : ^^^^^^^
1763+
>isAnimal : (something: Something) => something is Animal
1764+
> : ^ ^^ ^^^^^
1765+
>t : Something
1766+
> : ^^^^^^^^^
1767+
}
1768+
1769+
function negative(t: Something) {
1770+
>negative : (t: Something) => t is Rock
1771+
> : ^ ^^ ^^^^^^^^^^^^^^
1772+
>t : Something
1773+
> : ^^^^^^^^^
1774+
1775+
return !isAnimal(t)
1776+
>!isAnimal(t) : boolean
1777+
> : ^^^^^^^
1778+
>isAnimal(t) : boolean
1779+
> : ^^^^^^^
1780+
>isAnimal : (something: Something) => something is Animal
1781+
> : ^ ^^ ^^^^^
1782+
>t : Something
1783+
> : ^^^^^^^^^
1784+
}
1785+

tests/cases/compiler/inferTypePredicates.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,26 @@ function isEmptyString(x: unknown) {
289289
const rv = x === "";
290290
return rv satisfies boolean;
291291
}
292+
293+
// https://github.com/microsoft/TypeScript/issues/58996
294+
type Animal = {
295+
breath: true,
296+
};
297+
298+
type Rock = {
299+
breath: false,
300+
};
301+
302+
type Something = Animal | Rock;
303+
304+
function isAnimal(something: Something): something is Animal {
305+
return something.breath
306+
}
307+
308+
function positive(t: Something) {
309+
return isAnimal(t)
310+
}
311+
312+
function negative(t: Something) {
313+
return !isAnimal(t)
314+
}

0 commit comments

Comments
 (0)