Skip to content

Port "Narrow types by satisfies expressions" #1047

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

Merged
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
4 changes: 2 additions & 2 deletions internal/checker/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func (c *Checker) narrowType(f *FlowState, t *Type, expr *ast.Node, assumeTrue b
return c.narrowTypeByTruthiness(f, t, expr, assumeTrue)
case ast.KindCallExpression:
return c.narrowTypeByCallExpression(f, t, expr, assumeTrue)
case ast.KindParenthesizedExpression, ast.KindNonNullExpression:
case ast.KindParenthesizedExpression, ast.KindNonNullExpression, ast.KindSatisfiesExpression:
return c.narrowType(f, t, expr.Expression(), assumeTrue)
case ast.KindBinaryExpression:
return c.narrowTypeByBinaryExpression(f, t, expr.AsBinaryExpression(), assumeTrue)
Expand Down Expand Up @@ -1572,7 +1572,7 @@ func (c *Checker) isMatchingReference(source *ast.Node, target *ast.Node) bool {
return target.Kind == ast.KindThisKeyword
case ast.KindSuperKeyword:
return target.Kind == ast.KindSuperKeyword
case ast.KindNonNullExpression, ast.KindParenthesizedExpression:
case ast.KindNonNullExpression, ast.KindParenthesizedExpression, ast.KindSatisfiesExpression:
return c.isMatchingReference(source.Expression(), target)
case ast.KindPropertyAccessExpression, ast.KindElementAccessExpression:
if sourcePropertyName, ok := c.getAccessedPropertyName(source); ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ inferTypePredicates.ts(115,7): error TS2322: Type 'string | number' is not assig
inferTypePredicates.ts(133,7): error TS2322: Type 'object' is not assignable to type 'Date'.
Type '{}' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.
inferTypePredicates.ts(205,7): error TS2741: Property 'z' is missing in type 'C1' but required in type 'C2'.
inferTypePredicates.ts(281,7): error TS2322: Type '(number | null)[]' is not assignable to type 'number[]'.
Type 'number | null' is not assignable to type 'number'.
Type 'null' is not assignable to type 'number'.


==== inferTypePredicates.ts (12 errors) ====
==== inferTypePredicates.ts (11 errors) ====
// https://github.com/microsoft/TypeScript/issues/16069

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,9 @@
+inferTypePredicates.ts(133,7): error TS2322: Type 'object' is not assignable to type 'Date'.
+ Type '{}' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.
inferTypePredicates.ts(205,7): error TS2741: Property 'z' is missing in type 'C1' but required in type 'C2'.
-
-
-==== inferTypePredicates.ts (11 errors) ====
+inferTypePredicates.ts(281,7): error TS2322: Type '(number | null)[]' is not assignable to type 'number[]'.
+ Type 'number | null' is not assignable to type 'number'.
+ Type 'null' is not assignable to type 'number'.
+
+
+==== inferTypePredicates.ts (12 errors) ====
// https://github.com/microsoft/TypeScript/issues/16069

const numsOrNull = [1, 2, 3, 4, null];
@@= skipped -167, +171 lines =@@


@@= skipped -167, +168 lines =@@
if (flakyIsDate(maybeDate)) {
let t: Date = maybeDate; // should error
~
Expand All @@ -29,15 +19,4 @@
+!!! error TS2322: Type '{}' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.
} else {
let t: object = maybeDate; // should ok
}
@@= skipped -152, +153 lines =@@

// https://github.com/microsoft/TypeScript/issues/60778
const arrTest: Array<number> = [1, 2, null, 3].filter(
+ ~~~~~~~
+!!! error TS2322: Type '(number | null)[]' is not assignable to type 'number[]'.
+!!! error TS2322: Type 'number | null' is not assignable to type 'number'.
+!!! error TS2322: Type 'null' is not assignable to type 'number'.
(x) => (x != null) satisfies boolean,
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,4 @@ declare const foobarPred: (fb: {
};
// https://github.com/microsoft/TypeScript/issues/60778
declare const arrTest: Array<number>;
declare function isEmptyString(x: unknown): boolean;
declare function isEmptyString(x: unknown): x is "";
Original file line number Diff line number Diff line change
Expand Up @@ -426,5 +426,4 @@
};
+// https://github.com/microsoft/TypeScript/issues/60778
declare const arrTest: Array<number>;
-declare function isEmptyString(x: unknown): x is "";
+declare function isEmptyString(x: unknown): boolean;
declare function isEmptyString(x: unknown): x is "";
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ if (foobarPred(foobar)) {
// https://github.com/microsoft/TypeScript/issues/60778
const arrTest: Array<number> = [1, 2, null, 3].filter(
>arrTest : number[]
>[1, 2, null, 3].filter( (x) => (x != null) satisfies boolean,) : (number | null)[]
>[1, 2, null, 3].filter( (x) => (x != null) satisfies boolean,) : number[]
>[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)[]; }
>[1, 2, null, 3] : (number | null)[]
>1 : 1
Expand All @@ -1025,7 +1025,7 @@ const arrTest: Array<number> = [1, 2, null, 3].filter(
>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)[]; }

(x) => (x != null) satisfies boolean,
>(x) => (x != null) satisfies boolean : (x: number | null) => boolean
>(x) => (x != null) satisfies boolean : (x: number | null) => x is number
>x : number | null
>(x != null) satisfies boolean : boolean
>(x != null) : boolean
Expand All @@ -1035,7 +1035,7 @@ const arrTest: Array<number> = [1, 2, null, 3].filter(
);

function isEmptyString(x: unknown) {
>isEmptyString : (x: unknown) => boolean
>isEmptyString : (x: unknown) => x is ""
>x : unknown

const rv = x === "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,4 @@
+>foobarPred : (fb: { type: "foo"; foo: number; } | { type: "bar"; bar: string; }) => fb is { type: "foo"; foo: number; }
>foobar : { type: "foo"; foo: number; } | { type: "bar"; bar: string; }

foobar.foo;
@@= skipped -12, +12 lines =@@
// https://github.com/microsoft/TypeScript/issues/60778
const arrTest: Array<number> = [1, 2, null, 3].filter(
>arrTest : number[]
->[1, 2, null, 3].filter( (x) => (x != null) satisfies boolean,) : number[]
+>[1, 2, null, 3].filter( (x) => (x != null) satisfies boolean,) : (number | null)[]
>[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)[]; }
>[1, 2, null, 3] : (number | null)[]
>1 : 1
@@= skipped -9, +9 lines =@@
>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)[]; }

(x) => (x != null) satisfies boolean,
->(x) => (x != null) satisfies boolean : (x: number | null) => x is number
+>(x) => (x != null) satisfies boolean : (x: number | null) => boolean
>x : number | null
>(x != null) satisfies boolean : boolean
>(x != null) : boolean
@@= skipped -10, +10 lines =@@
);

function isEmptyString(x: unknown) {
->isEmptyString : (x: unknown) => x is ""
+>isEmptyString : (x: unknown) => boolean
>x : unknown

const rv = x === "";
foobar.foo;