Skip to content

Commit 3f44634

Browse files
authored
Port "Narrow types by satisfies expressions" (#1047)
1 parent c933b88 commit 3f44634

File tree

7 files changed

+13
-69
lines changed

7 files changed

+13
-69
lines changed

internal/checker/flow.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func (c *Checker) narrowType(f *FlowState, t *Type, expr *ast.Node, assumeTrue b
388388
return c.narrowTypeByTruthiness(f, t, expr, assumeTrue)
389389
case ast.KindCallExpression:
390390
return c.narrowTypeByCallExpression(f, t, expr, assumeTrue)
391-
case ast.KindParenthesizedExpression, ast.KindNonNullExpression:
391+
case ast.KindParenthesizedExpression, ast.KindNonNullExpression, ast.KindSatisfiesExpression:
392392
return c.narrowType(f, t, expr.Expression(), assumeTrue)
393393
case ast.KindBinaryExpression:
394394
return c.narrowTypeByBinaryExpression(f, t, expr.AsBinaryExpression(), assumeTrue)
@@ -1575,7 +1575,7 @@ func (c *Checker) isMatchingReference(source *ast.Node, target *ast.Node) bool {
15751575
return target.Kind == ast.KindThisKeyword
15761576
case ast.KindSuperKeyword:
15771577
return target.Kind == ast.KindSuperKeyword
1578-
case ast.KindNonNullExpression, ast.KindParenthesizedExpression:
1578+
case ast.KindNonNullExpression, ast.KindParenthesizedExpression, ast.KindSatisfiesExpression:
15791579
return c.isMatchingReference(source.Expression(), target)
15801580
case ast.KindPropertyAccessExpression, ast.KindElementAccessExpression:
15811581
if sourcePropertyName, ok := c.getAccessedPropertyName(source); ok {

testdata/baselines/reference/submodule/compiler/inferTypePredicates.errors.txt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ inferTypePredicates.ts(115,7): error TS2322: Type 'string | number' is not assig
2020
inferTypePredicates.ts(133,7): error TS2322: Type 'object' is not assignable to type 'Date'.
2121
Type '{}' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.
2222
inferTypePredicates.ts(205,7): error TS2741: Property 'z' is missing in type 'C1' but required in type 'C2'.
23-
inferTypePredicates.ts(281,7): error TS2322: Type '(number | null)[]' is not assignable to type 'number[]'.
24-
Type 'number | null' is not assignable to type 'number'.
25-
Type 'null' is not assignable to type 'number'.
2623

2724

28-
==== inferTypePredicates.ts (12 errors) ====
25+
==== inferTypePredicates.ts (11 errors) ====
2926
// https://github.com/microsoft/TypeScript/issues/16069
3027

3128
const numsOrNull = [1, 2, 3, 4, null];
@@ -341,10 +338,6 @@ inferTypePredicates.ts(281,7): error TS2322: Type '(number | null)[]' is not ass
341338

342339
// https://github.com/microsoft/TypeScript/issues/60778
343340
const arrTest: Array<number> = [1, 2, null, 3].filter(
344-
~~~~~~~
345-
!!! error TS2322: Type '(number | null)[]' is not assignable to type 'number[]'.
346-
!!! error TS2322: Type 'number | null' is not assignable to type 'number'.
347-
!!! error TS2322: Type 'null' is not assignable to type 'number'.
348341
(x) => (x != null) satisfies boolean,
349342
);
350343

testdata/baselines/reference/submodule/compiler/inferTypePredicates.errors.txt.diff

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,9 @@
88
+inferTypePredicates.ts(133,7): error TS2322: Type 'object' is not assignable to type 'Date'.
99
+ Type '{}' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.
1010
inferTypePredicates.ts(205,7): error TS2741: Property 'z' is missing in type 'C1' but required in type 'C2'.
11-
-
12-
-
13-
-==== inferTypePredicates.ts (11 errors) ====
14-
+inferTypePredicates.ts(281,7): error TS2322: Type '(number | null)[]' is not assignable to type 'number[]'.
15-
+ Type 'number | null' is not assignable to type 'number'.
16-
+ Type 'null' is not assignable to type 'number'.
17-
+
18-
+
19-
+==== inferTypePredicates.ts (12 errors) ====
20-
// https://github.com/microsoft/TypeScript/issues/16069
21-
22-
const numsOrNull = [1, 2, 3, 4, null];
23-
@@= skipped -167, +171 lines =@@
11+
12+
13+
@@= skipped -167, +168 lines =@@
2414
if (flakyIsDate(maybeDate)) {
2515
let t: Date = maybeDate; // should error
2616
~
@@ -29,15 +19,4 @@
2919
+!!! error TS2322: Type '{}' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.
3020
} else {
3121
let t: object = maybeDate; // should ok
32-
}
33-
@@= skipped -152, +153 lines =@@
34-
35-
// https://github.com/microsoft/TypeScript/issues/60778
36-
const arrTest: Array<number> = [1, 2, null, 3].filter(
37-
+ ~~~~~~~
38-
+!!! error TS2322: Type '(number | null)[]' is not assignable to type 'number[]'.
39-
+!!! error TS2322: Type 'number | null' is not assignable to type 'number'.
40-
+!!! error TS2322: Type 'null' is not assignable to type 'number'.
41-
(x) => (x != null) satisfies boolean,
42-
);
43-
22+
}

testdata/baselines/reference/submodule/compiler/inferTypePredicates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,4 +631,4 @@ declare const foobarPred: (fb: {
631631
};
632632
// https://github.com/microsoft/TypeScript/issues/60778
633633
declare const arrTest: Array<number>;
634-
declare function isEmptyString(x: unknown): boolean;
634+
declare function isEmptyString(x: unknown): x is "";

testdata/baselines/reference/submodule/compiler/inferTypePredicates.js.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,5 +426,4 @@
426426
};
427427
+// https://github.com/microsoft/TypeScript/issues/60778
428428
declare const arrTest: Array<number>;
429-
-declare function isEmptyString(x: unknown): x is "";
430-
+declare function isEmptyString(x: unknown): boolean;
429+
declare function isEmptyString(x: unknown): x is "";

testdata/baselines/reference/submodule/compiler/inferTypePredicates.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ if (foobarPred(foobar)) {
10161016
// https://github.com/microsoft/TypeScript/issues/60778
10171017
const arrTest: Array<number> = [1, 2, null, 3].filter(
10181018
>arrTest : number[]
1019-
>[1, 2, null, 3].filter( (x) => (x != null) satisfies boolean,) : (number | null)[]
1019+
>[1, 2, null, 3].filter( (x) => (x != null) satisfies boolean,) : number[]
10201020
>[1, 2, null, 3].filter : { <S extends number | null>(predicate: (value: number | null, index: number, array: (number | null)[]) => value is S, thisArg?: any): S[]; (predicate: (value: number | null, index: number, array: (number | null)[]) => unknown, thisArg?: any): (number | null)[]; }
10211021
>[1, 2, null, 3] : (number | null)[]
10221022
>1 : 1
@@ -1025,7 +1025,7 @@ const arrTest: Array<number> = [1, 2, null, 3].filter(
10251025
>filter : { <S extends number | null>(predicate: (value: number | null, index: number, array: (number | null)[]) => value is S, thisArg?: any): S[]; (predicate: (value: number | null, index: number, array: (number | null)[]) => unknown, thisArg?: any): (number | null)[]; }
10261026

10271027
(x) => (x != null) satisfies boolean,
1028-
>(x) => (x != null) satisfies boolean : (x: number | null) => boolean
1028+
>(x) => (x != null) satisfies boolean : (x: number | null) => x is number
10291029
>x : number | null
10301030
>(x != null) satisfies boolean : boolean
10311031
>(x != null) : boolean
@@ -1035,7 +1035,7 @@ const arrTest: Array<number> = [1, 2, null, 3].filter(
10351035
);
10361036

10371037
function isEmptyString(x: unknown) {
1038-
>isEmptyString : (x: unknown) => boolean
1038+
>isEmptyString : (x: unknown) => x is ""
10391039
>x : unknown
10401040

10411041
const rv = x === "";

testdata/baselines/reference/submodule/compiler/inferTypePredicates.types.diff

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -141,31 +141,4 @@
141141
+>foobarPred : (fb: { type: "foo"; foo: number; } | { type: "bar"; bar: string; }) => fb is { type: "foo"; foo: number; }
142142
>foobar : { type: "foo"; foo: number; } | { type: "bar"; bar: string; }
143143

144-
foobar.foo;
145-
@@= skipped -12, +12 lines =@@
146-
// https://github.com/microsoft/TypeScript/issues/60778
147-
const arrTest: Array<number> = [1, 2, null, 3].filter(
148-
>arrTest : number[]
149-
->[1, 2, null, 3].filter( (x) => (x != null) satisfies boolean,) : number[]
150-
+>[1, 2, null, 3].filter( (x) => (x != null) satisfies boolean,) : (number | null)[]
151-
>[1, 2, null, 3].filter : { <S extends number | null>(predicate: (value: number | null, index: number, array: (number | null)[]) => value is S, thisArg?: any): S[]; (predicate: (value: number | null, index: number, array: (number | null)[]) => unknown, thisArg?: any): (number | null)[]; }
152-
>[1, 2, null, 3] : (number | null)[]
153-
>1 : 1
154-
@@= skipped -9, +9 lines =@@
155-
>filter : { <S extends number | null>(predicate: (value: number | null, index: number, array: (number | null)[]) => value is S, thisArg?: any): S[]; (predicate: (value: number | null, index: number, array: (number | null)[]) => unknown, thisArg?: any): (number | null)[]; }
156-
157-
(x) => (x != null) satisfies boolean,
158-
->(x) => (x != null) satisfies boolean : (x: number | null) => x is number
159-
+>(x) => (x != null) satisfies boolean : (x: number | null) => boolean
160-
>x : number | null
161-
>(x != null) satisfies boolean : boolean
162-
>(x != null) : boolean
163-
@@= skipped -10, +10 lines =@@
164-
);
165-
166-
function isEmptyString(x: unknown) {
167-
->isEmptyString : (x: unknown) => x is ""
168-
+>isEmptyString : (x: unknown) => boolean
169-
>x : unknown
170-
171-
const rv = x === "";
144+
foobar.foo;

0 commit comments

Comments
 (0)