Skip to content

Commit 407687a

Browse files
1 parent 3163fe7 commit 407687a

File tree

6 files changed

+218
-3
lines changed

6 files changed

+218
-3
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38409,7 +38409,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3840938409
// "x is T" means that x is T if and only if it returns true. If it returns false then x is not T.
3841038410
// 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`.
3841138411
const falseCondition = createFlowNode(FlowFlags.FalseCondition, expr, antecedent);
38412-
const falseSubtype = getFlowTypeOfReference(param.name, initType, trueType, func, falseCondition);
38412+
const falseSubtype = getReducedType(getFlowTypeOfReference(param.name, initType, trueType, func, falseCondition));
3841338413
return falseSubtype.flags & TypeFlags.Never ? trueType : undefined;
3841438414
}
3841538415

tests/baselines/reference/inferTypePredicates.errors.txt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,26 @@ inferTypePredicates.ts(205,7): error TS2741: Property 'z' is missing in type 'C1
325325
if (foobarPred(foobar)) {
326326
foobar.foo;
327327
}
328-
328+
329+
// https://github.com/microsoft/TypeScript/issues/58996
330+
type Animal = {
331+
breath: true,
332+
};
333+
334+
type Rock = {
335+
breath: false,
336+
};
337+
338+
type Something = Animal | Rock;
339+
340+
function isAnimal(something: Something): something is Animal {
341+
return something.breath
342+
}
343+
344+
function positive(t: Something) {
345+
return isAnimal(t)
346+
}
347+
348+
function negative(t: Something) {
349+
return !isAnimal(t)
350+
}

tests/baselines/reference/inferTypePredicates.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,29 @@ const foobarPred = (fb: typeof foobar) => fb.type === "foo";
279279
if (foobarPred(foobar)) {
280280
foobar.foo;
281281
}
282-
282+
283+
// https://github.com/microsoft/TypeScript/issues/58996
284+
type Animal = {
285+
breath: true,
286+
};
287+
288+
type Rock = {
289+
breath: false,
290+
};
291+
292+
type Something = Animal | Rock;
293+
294+
function isAnimal(something: Something): something is Animal {
295+
return something.breath
296+
}
297+
298+
function positive(t: Something) {
299+
return isAnimal(t)
300+
}
301+
302+
function negative(t: Something) {
303+
return !isAnimal(t)
304+
}
283305

284306
//// [inferTypePredicates.js]
285307
// https://github.com/microsoft/TypeScript/issues/16069
@@ -538,6 +560,15 @@ var foobarPred = function (fb) { return fb.type === "foo"; };
538560
if (foobarPred(foobar)) {
539561
foobar.foo;
540562
}
563+
function isAnimal(something) {
564+
return something.breath;
565+
}
566+
function positive(t) {
567+
return isAnimal(t);
568+
}
569+
function negative(t) {
570+
return !isAnimal(t);
571+
}
541572

542573

543574
//// [inferTypePredicates.d.ts]
@@ -630,3 +661,13 @@ declare const foobarPred: (fb: typeof foobar) => fb is {
630661
type: "foo";
631662
foo: number;
632663
};
664+
type Animal = {
665+
breath: true;
666+
};
667+
type Rock = {
668+
breath: false;
669+
};
670+
type Something = Animal | Rock;
671+
declare function isAnimal(something: Something): something is Animal;
672+
declare function positive(t: Something): t is Animal;
673+
declare function negative(t: Something): t is Rock;

tests/baselines/reference/inferTypePredicates.symbols

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,3 +777,57 @@ if (foobarPred(foobar)) {
777777
>foo : Symbol(foo, Decl(inferTypePredicates.ts, 271, 18))
778778
}
779779

780+
// https://github.com/microsoft/TypeScript/issues/58996
781+
type Animal = {
782+
>Animal : Symbol(Animal, Decl(inferTypePredicates.ts, 277, 1))
783+
784+
breath: true,
785+
>breath : Symbol(breath, Decl(inferTypePredicates.ts, 280, 15))
786+
787+
};
788+
789+
type Rock = {
790+
>Rock : Symbol(Rock, Decl(inferTypePredicates.ts, 282, 2))
791+
792+
breath: false,
793+
>breath : Symbol(breath, Decl(inferTypePredicates.ts, 284, 13))
794+
795+
};
796+
797+
type Something = Animal | Rock;
798+
>Something : Symbol(Something, Decl(inferTypePredicates.ts, 286, 2))
799+
>Animal : Symbol(Animal, Decl(inferTypePredicates.ts, 277, 1))
800+
>Rock : Symbol(Rock, Decl(inferTypePredicates.ts, 282, 2))
801+
802+
function isAnimal(something: Something): something is Animal {
803+
>isAnimal : Symbol(isAnimal, Decl(inferTypePredicates.ts, 288, 31))
804+
>something : Symbol(something, Decl(inferTypePredicates.ts, 290, 18))
805+
>Something : Symbol(Something, Decl(inferTypePredicates.ts, 286, 2))
806+
>something : Symbol(something, Decl(inferTypePredicates.ts, 290, 18))
807+
>Animal : Symbol(Animal, Decl(inferTypePredicates.ts, 277, 1))
808+
809+
return something.breath
810+
>something.breath : Symbol(breath, Decl(inferTypePredicates.ts, 280, 15), Decl(inferTypePredicates.ts, 284, 13))
811+
>something : Symbol(something, Decl(inferTypePredicates.ts, 290, 18))
812+
>breath : Symbol(breath, Decl(inferTypePredicates.ts, 280, 15), Decl(inferTypePredicates.ts, 284, 13))
813+
}
814+
815+
function positive(t: Something) {
816+
>positive : Symbol(positive, Decl(inferTypePredicates.ts, 292, 1))
817+
>t : Symbol(t, Decl(inferTypePredicates.ts, 294, 18))
818+
>Something : Symbol(Something, Decl(inferTypePredicates.ts, 286, 2))
819+
820+
return isAnimal(t)
821+
>isAnimal : Symbol(isAnimal, Decl(inferTypePredicates.ts, 288, 31))
822+
>t : Symbol(t, Decl(inferTypePredicates.ts, 294, 18))
823+
}
824+
825+
function negative(t: Something) {
826+
>negative : Symbol(negative, Decl(inferTypePredicates.ts, 296, 1))
827+
>t : Symbol(t, Decl(inferTypePredicates.ts, 298, 18))
828+
>Something : Symbol(Something, Decl(inferTypePredicates.ts, 286, 2))
829+
830+
return !isAnimal(t)
831+
>isAnimal : Symbol(isAnimal, Decl(inferTypePredicates.ts, 288, 31))
832+
>t : Symbol(t, Decl(inferTypePredicates.ts, 298, 18))
833+
}

tests/baselines/reference/inferTypePredicates.types

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,3 +1649,78 @@ if (foobarPred(foobar)) {
16491649
> : ^^^^^^
16501650
}
16511651

1652+
// https://github.com/microsoft/TypeScript/issues/58996
1653+
type Animal = {
1654+
>Animal : Animal
1655+
> : ^^^^^^
1656+
1657+
breath: true,
1658+
>breath : true
1659+
> : ^^^^
1660+
>true : true
1661+
> : ^^^^
1662+
1663+
};
1664+
1665+
type Rock = {
1666+
>Rock : Rock
1667+
> : ^^^^
1668+
1669+
breath: false,
1670+
>breath : false
1671+
> : ^^^^^
1672+
>false : false
1673+
> : ^^^^^
1674+
1675+
};
1676+
1677+
type Something = Animal | Rock;
1678+
>Something : Something
1679+
> : ^^^^^^^^^
1680+
1681+
function isAnimal(something: Something): something is Animal {
1682+
>isAnimal : (something: Something) => something is Animal
1683+
> : ^ ^^ ^^^^^
1684+
>something : Something
1685+
> : ^^^^^^^^^
1686+
1687+
return something.breath
1688+
>something.breath : boolean
1689+
> : ^^^^^^^
1690+
>something : Something
1691+
> : ^^^^^^^^^
1692+
>breath : boolean
1693+
> : ^^^^^^^
1694+
}
1695+
1696+
function positive(t: Something) {
1697+
>positive : (t: Something) => t is Animal
1698+
> : ^ ^^ ^^^^^^^^^^^^^^^^
1699+
>t : Something
1700+
> : ^^^^^^^^^
1701+
1702+
return isAnimal(t)
1703+
>isAnimal(t) : boolean
1704+
> : ^^^^^^^
1705+
>isAnimal : (something: Something) => something is Animal
1706+
> : ^ ^^ ^^^^^
1707+
>t : Something
1708+
> : ^^^^^^^^^
1709+
}
1710+
1711+
function negative(t: Something) {
1712+
>negative : (t: Something) => t is Rock
1713+
> : ^ ^^ ^^^^^^^^^^^^^^
1714+
>t : Something
1715+
> : ^^^^^^^^^
1716+
1717+
return !isAnimal(t)
1718+
>!isAnimal(t) : boolean
1719+
> : ^^^^^^^
1720+
>isAnimal(t) : boolean
1721+
> : ^^^^^^^
1722+
>isAnimal : (something: Something) => something is Animal
1723+
> : ^ ^^ ^^^^^
1724+
>t : Something
1725+
> : ^^^^^^^^^
1726+
}

tests/cases/compiler/inferTypePredicates.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,26 @@ const foobarPred = (fb: typeof foobar) => fb.type === "foo";
279279
if (foobarPred(foobar)) {
280280
foobar.foo;
281281
}
282+
283+
// https://github.com/microsoft/TypeScript/issues/58996
284+
type Animal = {
285+
breath: true,
286+
};
287+
288+
type Rock = {
289+
breath: false,
290+
};
291+
292+
type Something = Animal | Rock;
293+
294+
function isAnimal(something: Something): something is Animal {
295+
return something.breath
296+
}
297+
298+
function positive(t: Something) {
299+
return isAnimal(t)
300+
}
301+
302+
function negative(t: Something) {
303+
return !isAnimal(t)
304+
}

0 commit comments

Comments
 (0)