Skip to content
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

Fixed an issue with satisfies "resolving" generic types of references #60710

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 9 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,7 @@ export const enum CheckMode {
// e.g. in `const { a, ...rest } = foo`, when checking the type of `foo` to determine the type of `rest`,
// we need to preserve generic types instead of substituting them for constraints
TypeOnly = 1 << 6, // Called from getTypeOfExpression, diagnostics may be omitted
SatisfiesOutput = 1 << 7, // Used to get output type of a satisfies expression unaffected by narrowable types for references, used in combination with `ContextFlags.SkipSatisfies`
}

/** @internal */
Expand Down Expand Up @@ -29800,17 +29801,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
!!(type.flags & TypeFlags.Instantiable && !maybeTypeOfKind(getBaseConstraintOrType(type), TypeFlags.Nullable));
}

function hasContextualTypeWithNoGenericTypes(node: Node, checkMode: CheckMode | undefined) {
function hasContextualTypeWithNoGenericTypes(node: Node, checkMode = CheckMode.Normal) {
// Computing the contextual type for a child of a JSX element involves resolving the type of the
// element's tag name, so we exclude that here to avoid circularities.
// If check mode has `CheckMode.RestBindingElement`, we skip binding pattern contextual types,
// as we want the type of a rest element to be generic when possible.
const contextualType = (isIdentifier(node) || isPropertyAccessExpression(node) || isElementAccessExpression(node)) &&
!((isJsxOpeningElement(node.parent) || isJsxSelfClosingElement(node.parent)) && node.parent.tagName === node) &&
(checkMode && checkMode & CheckMode.RestBindingElement ?
getContextualType(node, ContextFlags.SkipBindingPatterns)
: getContextualType(node, /*contextFlags*/ undefined));
return contextualType && !isGenericType(contextualType);
getContextualType(node, (checkMode & CheckMode.RestBindingElement ? ContextFlags.SkipBindingPatterns : 0) | (checkMode & CheckMode.SatisfiesOutput ? ContextFlags.SkipSatisfies : 0));
return contextualType && !(contextualType.flags & TypeFlags.Any) && !isGenericType(contextualType);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The added !(contextualType.flags & TypeFlags.Any) bit here isn't exactly related to satisfies at all. I have added it to account for auto types, that affected all the baselines that had any as the contextual type in a few places. It also affected those codefixes for missing function declarations since the error type (a special kind of any) was the contextual type for those arguments here. I think those changes are positive

}

function getNarrowableTypeForReference(type: Type, reference: Node, checkMode?: CheckMode, forReturnTypeNarrowing?: boolean) {
Expand Down Expand Up @@ -32190,6 +32189,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.NonNullExpression:
return getContextualType(parent as NonNullExpression, contextFlags);
case SyntaxKind.SatisfiesExpression:
if (contextFlags && contextFlags & ContextFlags.SkipSatisfies) {
return getContextualType(parent as SatisfiesExpression, contextFlags);
}
return getTypeFromTypeNode((parent as SatisfiesExpression).type);
case SyntaxKind.ExportAssignment:
return tryGetTypeFromEffectiveTypeNode(parent as ExportAssignment);
Expand Down Expand Up @@ -37573,15 +37575,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return checkSatisfiesExpressionWorker(node.expression, node.type);
}

function checkSatisfiesExpressionWorker(expression: Expression, target: TypeNode, checkMode?: CheckMode) {
function checkSatisfiesExpressionWorker(expression: Expression, target: TypeNode, checkMode = CheckMode.Normal) {
const exprType = checkExpression(expression, checkMode);
const targetType = getTypeFromTypeNode(target);
if (isErrorType(targetType)) {
return targetType;
}
const errorNode = findAncestor(target.parent, n => n.kind === SyntaxKind.SatisfiesExpression || n.kind === SyntaxKind.JSDocSatisfiesTag);
checkTypeAssignableToAndOptionallyElaborate(exprType, targetType, errorNode, expression, Diagnostics.Type_0_does_not_satisfy_the_expected_type_1);
return exprType;
return checkExpression(expression, checkMode | CheckMode.SatisfiesOutput);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exprType is used to check the assignability, it may contain "resolved" types for narrowable references. So the assignability works like it works in:

const x: "A" = genericReferenceNarrowedDownToA;

But the output type is different because we should propagate that "resolved" type in:

// actual on main: `"A"`
// expected: `AB extends "A" | "B"`
const x = genericReferenceNarrowedDownToA satisfies "A";

}

function checkMetaProperty(node: MetaProperty): Type {
Expand Down
9 changes: 5 additions & 4 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5462,11 +5462,12 @@ export const enum IntersectionFlags {
// dprint-ignore
/** @internal */
export const enum ContextFlags {
None = 0,
Signature = 1 << 0, // Obtaining contextual signature
NoConstraints = 1 << 1, // Don't obtain type variable constraints
Completions = 1 << 2, // Ignore inference to current node and parent nodes out to the containing call for completions
None = 0,
Signature = 1 << 0, // Obtaining contextual signature
NoConstraints = 1 << 1, // Don't obtain type variable constraints
Completions = 1 << 2, // Ignore inference to current node and parent nodes out to the containing call for completions
SkipBindingPatterns = 1 << 3, // Ignore contextual types applied by binding patterns
SkipSatisfies = 1 << 4, // Ignore contextual types applied by `satisfies` types
}

// NOTE: If modifying this enum, must modify `TypeFormatFlags` too!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [tests/cases/compiler/controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts] ////

=== controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts ===
const foo_autoType = <T extends 1 | 2>(bar: T) => {
>foo_autoType : Symbol(foo_autoType, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 0, 5))
>T : Symbol(T, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 0, 22))
>bar : Symbol(bar, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 0, 39))
>T : Symbol(T, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 0, 22))

let test1;
>test1 : Symbol(test1, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 1, 5))

test1 = bar;
>test1 : Symbol(test1, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 1, 5))
>bar : Symbol(bar, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 0, 39))

return test1;
>test1 : Symbol(test1, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 1, 5))

};

const foo_autoArrayType = <T extends 1 | 2>(bar: T) => {
>foo_autoArrayType : Symbol(foo_autoArrayType, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 6, 5))
>T : Symbol(T, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 6, 27))
>bar : Symbol(bar, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 6, 44))
>T : Symbol(T, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 6, 27))

let test1 = [];
>test1 : Symbol(test1, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 7, 5))

test1.push(bar);
>test1.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
>test1 : Symbol(test1, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 7, 5))
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
>bar : Symbol(bar, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 6, 44))

return test1;
>test1 : Symbol(test1, Decl(controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts, 7, 5))

};

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//// [tests/cases/compiler/controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts] ////

=== controlFlowGenericTypesNarrowableReferencesWithAutoTypes1.ts ===
const foo_autoType = <T extends 1 | 2>(bar: T) => {
>foo_autoType : <T extends 1 | 2>(bar: T) => T
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^
><T extends 1 | 2>(bar: T) => { let test1; test1 = bar; return test1;} : <T extends 1 | 2>(bar: T) => T
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^
>bar : T
> : ^

let test1;
>test1 : any

test1 = bar;
>test1 = bar : T
> : ^
>test1 : any
>bar : T
> : ^

return test1;
>test1 : T
> : ^

};

const foo_autoArrayType = <T extends 1 | 2>(bar: T) => {
>foo_autoArrayType : <T extends 1 | 2>(bar: T) => T[]
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^
><T extends 1 | 2>(bar: T) => { let test1 = []; test1.push(bar); return test1;} : <T extends 1 | 2>(bar: T) => T[]
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^
>bar : T
> : ^

let test1 = [];
>test1 : any[]
> : ^^^^^
>[] : never[]
> : ^^^^^^^

test1.push(bar);
>test1.push(bar) : number
> : ^^^^^^
>test1.push : (...items: any[]) => number
> : ^^^^ ^^^^^^^^^^^^
>test1 : any[]
> : ^^^^^
>push : (...items: any[]) => number
> : ^^^^ ^^^^^^^^^^^^
>bar : T
> : ^

return test1;
>test1 : T[]
> : ^^^

};

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
controlFlowGenericTypesNarrowableReferencesWithSatisfies1.ts(50,3): error TS2322: Type '{ readonly table: T; readonly id: "foo"; }' is not assignable to type 'RecordPointer<Table>'.
Type '{ readonly table: T; readonly id: "foo"; }' is not assignable to type '{ id: string; table: "space"; }'.
Types of property 'table' are incompatible.
Type 'T' is not assignable to type '"space"'.
Type 'Table' is not assignable to type '"space"'.
Type '"block"' is not assignable to type '"space"'.


==== controlFlowGenericTypesNarrowableReferencesWithSatisfies1.ts (1 errors) ====
// https://github.com/microsoft/TypeScript/issues/52394

const foo52394 = <T extends 1 | 2>(bar: T) => {
const a = bar satisfies any;
return a;
};

type MyType52394 = {
a: string;
b: "a" | "b" | "c" | "d" | "e";
};
const foo2_52394 = <T extends "a" | "b" | "c">(bar: T) =>
({
a: bar,
b: bar,
}) satisfies MyType52394;

type BoxState = "open" | "closed";

type Box = {
boxState: BoxState;
boxedObject: unknown;
};

function boxFactorySafe<BS extends BoxState>(
boxState: BS,
boxedObject: unknown,
) {
return {
boxState,
boxedObject,
} as const satisfies Box;
}

const safeBoxedObject = boxFactorySafe("open", "some value");

// https://github.com/microsoft/TypeScript/issues/60698

type Table = "block" | "collection" | "space";

type RecordPointer<T extends Table> = {
[T_ in T]: {
id: string;
table: T_;
};
}[T];

function g<T extends Table>(t: T): RecordPointer<Table> {
const x = { table: t, id: "foo" } as const satisfies RecordPointer<Table>;
return x; // error
~~~~~~
!!! error TS2322: Type '{ readonly table: T; readonly id: "foo"; }' is not assignable to type 'RecordPointer<Table>'.
!!! error TS2322: Type '{ readonly table: T; readonly id: "foo"; }' is not assignable to type '{ id: string; table: "space"; }'.
!!! error TS2322: Types of property 'table' are incompatible.
!!! error TS2322: Type 'T' is not assignable to type '"space"'.
!!! error TS2322: Type 'Table' is not assignable to type '"space"'.
!!! error TS2322: Type '"block"' is not assignable to type '"space"'.
}

export function bounceAndTakeIfA<AB extends "A" | "B">(value: AB) {
if (value === "A") {
const temp = value satisfies "A"
const takeA: "A" = value satisfies "A";
}
}

Loading
Loading