Skip to content

Commit 640c71d

Browse files
committed
Consider assertions invertible and create negated narrowings in false branches of assertion narrowings
1 parent 051571d commit 640c71d

46 files changed

Lines changed: 1062 additions & 393 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎tsc/internal/checker/flow.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,9 +981,11 @@ func (c *Checker) getNarrowedTypeWorker(t *Type, candidate *Type, assumeTrue boo
981981
t = c.unknownUnionType
982982
}
983983
trueType := c.getNarrowedType(t, candidate, true /*assumeTrue*/, false /*checkDerived*/)
984-
return c.recombineUnknownType(c.filterType(t, func(t *Type) bool {
984+
filtered := c.recombineUnknownType(c.filterType(t, func(t *Type) bool {
985985
return !c.isTypeSubsetOf(t, trueType)
986986
}))
987+
// Treat type predicates as invertible, even if they only recognize a subset of the candidate.
988+
return c.introduceNegationIntoNarrowedType(filtered, c.getReducedType(candidate))
987989
}
988990
if t.flags&TypeFlagsAnyOrUnknown != 0 {
989991
return candidate

‎tsc/testdata/baselines/reference/compiler/coAndContraVariantInferences2.types‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function f3(arr: readonly B[] | readonly C[]) {
106106
}
107107
else {
108108
arr; // readonly B[]
109-
>arr : readonly B[]
109+
>arr : readonly B[] & not readonly C[]
110110
}
111111
}
112112

‎tsc/testdata/baselines/reference/compiler/controlFlowFavorAssertedTypeThroughTypePredicate.types‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if (isObject1(obj1)) {
2525
}
2626
// check type after conditional block
2727
obj1;
28-
>obj1 : {}
28+
>obj1 : Record<string, unknown>
2929

3030
declare const obj2: {} | undefined;
3131
>obj2 : {} | undefined
@@ -45,7 +45,7 @@ if (isObject1(obj2)) {
4545
}
4646
// check type after conditional block
4747
obj2;
48-
>obj2 : {} | undefined
48+
>obj2 : Record<string, unknown> | undefined
4949

5050
declare function isObject2(value: unknown): value is {};
5151
>isObject2 : (value: unknown) => value is {}

‎tsc/testdata/baselines/reference/compiler/controlFlowUnionContainingTypeParameter1.types‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class TestClass<T> {
4646
>this.f : (v: number) => void
4747
>this : this
4848
>f : (v: number) => void
49-
>val : number
49+
>val : number & not T
5050
}
5151
}
5252

@@ -89,14 +89,14 @@ class TestClass2<T extends Date> {
8989
return;
9090
}
9191
val;
92-
>val : number
92+
>val : number & not T
9393

9494
this.f(val);
9595
>this.f(val) : void
9696
>this.f : (v: number) => void
9797
>this : this
9898
>f : (v: number) => void
99-
>val : number
99+
>val : number & not T
100100
}
101101
}
102102

‎tsc/testdata/baselines/reference/compiler/freshNegatedTypeWidening.types‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function assertWrappedNode(node: BaseNode) {
2727

2828
return node as WrappedNode;
2929
>node as WrappedNode : WrappedNode
30-
>node : BaseNode
30+
>node : BaseNode & not WrappedNode
3131
}
3232
return node;
3333
>node : WrappedNode
@@ -65,7 +65,7 @@ function skipWrappers(node: BaseNode) {
6565
>expression : BaseNode
6666
}
6767
return node;
68-
>node : BaseNode
68+
>node : BaseNode & not WrappedNode
6969
}
7070

7171
interface NamedNode extends BaseNode {
@@ -102,7 +102,7 @@ function skipSpecialNodes(node: BaseNode) {
102102
>0 : 0
103103
}
104104
return node;
105-
>node : BaseNode
105+
>node : BaseNode & not NamedNode & not WrappedNode
106106
}
107107

108108
function isOrdinaryNode(node: BaseNode) {

‎tsc/testdata/baselines/reference/compiler/genericCapturingFunctionNarrowing.types‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function needsToNarrowTheType<First extends { foo: string }, Second extends { ba
3030
>console : Console
3131
>log : (...data: any[]) => void
3232
>thing.bar : string
33-
>thing : Second
33+
>thing : Second & not First
3434
>bar : string
3535
}
3636

‎tsc/testdata/baselines/reference/compiler/inKeywordTypeguard(strict=false).types‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,14 +1102,14 @@ function test1<T extends any[] | Record<string, any>>(obj: T) {
11021102
>obj : any[] | Record<string, any>
11031103
>'length' in obj : boolean
11041104
>'length' : "length"
1105-
>obj : T
1105+
>obj : T & not any[]
11061106

11071107
obj; // T
11081108
>obj : T
11091109
}
11101110
else {
11111111
obj; // T
1112-
>obj : T
1112+
>obj : T & not any[]
11131113
}
11141114
}
11151115

@@ -1129,7 +1129,7 @@ function test2<T extends any[] | Record<string, any>>(obj: T) {
11291129
}
11301130
else {
11311131
obj; // T
1132-
>obj : T
1132+
>obj : T & not any[]
11331133
}
11341134
}
11351135

‎tsc/testdata/baselines/reference/compiler/inKeywordTypeguard(strict=true).types‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,14 +1102,14 @@ function test1<T extends any[] | Record<string, any>>(obj: T) {
11021102
>obj : any[] | Record<string, any>
11031103
>'length' in obj : boolean
11041104
>'length' : "length"
1105-
>obj : T
1105+
>obj : T & not any[]
11061106

11071107
obj; // T
11081108
>obj : T
11091109
}
11101110
else {
11111111
obj; // T
1112-
>obj : T
1112+
>obj : T & not any[]
11131113
}
11141114
}
11151115

@@ -1129,7 +1129,7 @@ function test2<T extends any[] | Record<string, any>>(obj: T) {
11291129
}
11301130
else {
11311131
obj; // T
1132-
>obj : T
1132+
>obj : T & not any[]
11331133
}
11341134
}
11351135

‎tsc/testdata/baselines/reference/compiler/inferTypePredicates.types‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ if (isDate(maybeDate)) {
548548
} else {
549549
let t: object = maybeDate; // should ok
550550
>t : object
551-
>maybeDate : object
551+
>maybeDate : object & not Date
552552
}
553553

554554
if (flakyIsDate(maybeDate)) {

‎tsc/testdata/baselines/reference/compiler/instanceofWithStructurallyIdenticalTypes.types‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ function foo2(x: C1 | C2 | C3): string {
9393
else if (isC2(x)) {
9494
>isC2(x) : boolean
9595
>isC2 : (c: C1 | C2 | C3) => c is C2
96-
>x : C2
96+
>x : C2 & not C1
9797

9898
return x.item[0];
9999
>x.item[0] : string
100100
>x.item : string[]
101-
>x : C2
101+
>x : C2 & not C1
102102
>item : string[]
103103
>0 : 0
104104
}

0 commit comments

Comments
 (0)