-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
|
@@ -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); | ||
} | ||
|
||
function getNarrowableTypeForReference(type: Type, reference: Node, checkMode?: CheckMode, forReturnTypeNarrowing?: boolean) { | ||
|
@@ -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); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,8 +160,8 @@ function g1<T extends Box<T> | undefined>(x: T) { | |
> : ^^^^^^^ | ||
>isBox : (x: any) => x is Box<unknown> | ||
> : ^ ^^ ^^^^^ | ||
>x : Box<T> | undefined | ||
> : ^^^^^^^^^^^^^^^^^^ | ||
>x : T | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all the baseline changes like this are caused by the fact that now |
||
> : ^ | ||
|
||
unbox(x); | ||
>unbox(x) : T | ||
|
@@ -212,8 +212,8 @@ function g3<T extends Box<T> | undefined>(x: T) { | |
> : ^^^^^^^ | ||
>isBox : (x: any) => x is Box<unknown> | ||
> : ^ ^^ ^^^^^ | ||
>x : Box<T> | undefined | ||
> : ^^^^^^^^^^^^^^^^^^ | ||
>x : T | ||
> : ^ | ||
|
||
unbox(x); // Error | ||
>unbox(x) : T | ||
|
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"; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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 tosatisfies
at all. I have added it to account for auto types, that affected all the baselines that hadany
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