diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2549639115215..caec310b705b3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11737,7 +11737,10 @@ namespace ts { else if ((isLiteralType(source) || source.flags & TypeFlags.String) && target.flags & TypeFlags.Index) { const empty = createEmptyObjectTypeFromStringLiteral(source); contravariant = !contravariant; + const savePriority = priority; + priority |= InferencePriority.LiteralKeyof; inferFromTypes(empty, (target as IndexType).type); + priority = savePriority; contravariant = !contravariant; } else if (source.flags & TypeFlags.IndexedAccess && target.flags & TypeFlags.IndexedAccess) { @@ -11980,6 +11983,30 @@ namespace ts { return candidates; } + function getContravariantInference(inference: InferenceInfo) { + return inference.priority & InferencePriority.PriorityImpliesCombination ? getIntersectionType(inference.contraCandidates) : getCommonSubtype(inference.contraCandidates); + } + + function getCovariantInference(inference: InferenceInfo, context: InferenceContext, signature: Signature) { + // Extract all object literal types and replace them with a single widened and normalized type. + const candidates = widenObjectLiteralCandidates(inference.candidates); + // We widen inferred literal types if + // all inferences were made to top-level occurrences of the type parameter, and + // the type parameter has no constraint or its constraint includes no primitive or literal types, and + // the type parameter was fixed during inference or does not occur at top-level in the return type. + const widenLiteralTypes = inference.topLevel && + !hasPrimitiveConstraint(inference.typeParameter) && + (inference.isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), inference.typeParameter)); + const baseCandidates = widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) : candidates; + // If all inferences were made from contravariant positions, infer a common subtype. Otherwise, if + // union types were requested or if all inferences were made from the return type position, infer a + // union type. Otherwise, infer a common supertype. + const unwidenedType = context.flags & InferenceFlags.InferUnionTypes || inference.priority & InferencePriority.PriorityImpliesCombination ? + getUnionType(baseCandidates, UnionReduction.Subtype) : + getCommonSupertype(baseCandidates); + return getWidenedType(unwidenedType); + } + function getInferredType(context: InferenceContext, index: number): Type { const inference = context.inferences[index]; let inferredType = inference.inferredType; @@ -11987,32 +12014,16 @@ namespace ts { const signature = context.signature; if (signature) { if (inference.candidates) { - // Extract all object literal types and replace them with a single widened and normalized type. - const candidates = widenObjectLiteralCandidates(inference.candidates); - // We widen inferred literal types if - // all inferences were made to top-level ocurrences of the type parameter, and - // the type parameter has no constraint or its constraint includes no primitive or literal types, and - // the type parameter was fixed during inference or does not occur at top-level in the return type. - const widenLiteralTypes = inference.topLevel && - !hasPrimitiveConstraint(inference.typeParameter) && - (inference.isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), inference.typeParameter)); - const baseCandidates = widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) : candidates; - // If all inferences were made from contravariant positions, infer a common subtype. Otherwise, if - // union types were requested or if all inferences were made from the return type position, infer a - // union type. Otherwise, infer a common supertype. - const unwidenedType = context.flags & InferenceFlags.InferUnionTypes || inference.priority & InferencePriority.PriorityImpliesUnion ? - getUnionType(baseCandidates, UnionReduction.Subtype) : - getCommonSupertype(baseCandidates); - inferredType = getWidenedType(unwidenedType); + inferredType = getCovariantInference(inference, context, signature); // If we have inferred 'never' but have contravariant candidates. To get a more specific type we // infer from the contravariant candidates instead. if (inferredType.flags & TypeFlags.Never && inference.contraCandidates) { - inferredType = getCommonSubtype(inference.contraCandidates); + inferredType = getContravariantInference(inference); } } else if (inference.contraCandidates) { // We only have contravariant inferences, infer the best common subtype of those - inferredType = getCommonSubtype(inference.contraCandidates); + inferredType = getContravariantInference(inference); } else if (context.flags & InferenceFlags.NoDefault) { // We use silentNeverType as the wildcard that signals no inferences. diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 42d8462cecdb7..d4699bfb7818f 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3920,10 +3920,11 @@ namespace ts { HomomorphicMappedType = 1 << 1, // Reverse inference for homomorphic mapped type MappedTypeConstraint = 1 << 2, // Reverse inference for mapped type ReturnType = 1 << 3, // Inference made from return type of generic function - NoConstraints = 1 << 4, // Don't infer from constraints of instantiable types - AlwaysStrict = 1 << 5, // Always use strict rules for contravariant inferences + LiteralKeyof = 1 << 4, // Inference made from a string literal to a keyof T + NoConstraints = 1 << 5, // Don't infer from constraints of instantiable types + AlwaysStrict = 1 << 6, // Always use strict rules for contravariant inferences - PriorityImpliesUnion = ReturnType | MappedTypeConstraint, // These priorities imply that the resulting type should be a union of all candidates + PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates } /* @internal */ diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 1048f338d922a..f7d74d4abbd57 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2240,9 +2240,10 @@ declare namespace ts { HomomorphicMappedType = 2, MappedTypeConstraint = 4, ReturnType = 8, - NoConstraints = 16, - AlwaysStrict = 32, - PriorityImpliesUnion = 12, + LiteralKeyof = 16, + NoConstraints = 32, + AlwaysStrict = 64, + PriorityImpliesCombination = 28, } interface JsFileExtensionInfo { extension: string; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 99a9bd99a6037..871b05a2132e3 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2240,9 +2240,10 @@ declare namespace ts { HomomorphicMappedType = 2, MappedTypeConstraint = 4, ReturnType = 8, - NoConstraints = 16, - AlwaysStrict = 32, - PriorityImpliesUnion = 12, + LiteralKeyof = 16, + NoConstraints = 32, + AlwaysStrict = 64, + PriorityImpliesCombination = 28, } interface JsFileExtensionInfo { extension: string; diff --git a/tests/baselines/reference/keyofInferenceIntersectsResults.js b/tests/baselines/reference/keyofInferenceIntersectsResults.js new file mode 100644 index 0000000000000..8267417ad422f --- /dev/null +++ b/tests/baselines/reference/keyofInferenceIntersectsResults.js @@ -0,0 +1,17 @@ +//// [keyofInferenceIntersectsResults.ts] +interface X { + a: string; + b: string; +} + +declare function foo(x: keyof T, y: keyof T): T; +declare function bar(x: keyof T, y: keyof T): T; + +const a = foo('a', 'b'); // compiles cleanly +const b = foo('a', 'b'); // also clean +const c = bar('a', 'b'); // still clean + +//// [keyofInferenceIntersectsResults.js] +var a = foo('a', 'b'); // compiles cleanly +var b = foo('a', 'b'); // also clean +var c = bar('a', 'b'); // still clean diff --git a/tests/baselines/reference/keyofInferenceIntersectsResults.symbols b/tests/baselines/reference/keyofInferenceIntersectsResults.symbols new file mode 100644 index 0000000000000..88340b821d2a8 --- /dev/null +++ b/tests/baselines/reference/keyofInferenceIntersectsResults.symbols @@ -0,0 +1,43 @@ +=== tests/cases/conformance/types/typeRelationships/typeInference/keyofInferenceIntersectsResults.ts === +interface X { +>X : Symbol(X, Decl(keyofInferenceIntersectsResults.ts, 0, 0)) + + a: string; +>a : Symbol(X.a, Decl(keyofInferenceIntersectsResults.ts, 0, 13)) + + b: string; +>b : Symbol(X.b, Decl(keyofInferenceIntersectsResults.ts, 1, 14)) +} + +declare function foo(x: keyof T, y: keyof T): T; +>foo : Symbol(foo, Decl(keyofInferenceIntersectsResults.ts, 3, 1)) +>T : Symbol(T, Decl(keyofInferenceIntersectsResults.ts, 5, 21)) +>X : Symbol(X, Decl(keyofInferenceIntersectsResults.ts, 0, 0)) +>x : Symbol(x, Decl(keyofInferenceIntersectsResults.ts, 5, 28)) +>T : Symbol(T, Decl(keyofInferenceIntersectsResults.ts, 5, 21)) +>y : Symbol(y, Decl(keyofInferenceIntersectsResults.ts, 5, 39)) +>T : Symbol(T, Decl(keyofInferenceIntersectsResults.ts, 5, 21)) +>T : Symbol(T, Decl(keyofInferenceIntersectsResults.ts, 5, 21)) + +declare function bar(x: keyof T, y: keyof T): T; +>bar : Symbol(bar, Decl(keyofInferenceIntersectsResults.ts, 5, 55)) +>T : Symbol(T, Decl(keyofInferenceIntersectsResults.ts, 6, 21)) +>x : Symbol(x, Decl(keyofInferenceIntersectsResults.ts, 6, 24)) +>T : Symbol(T, Decl(keyofInferenceIntersectsResults.ts, 6, 21)) +>y : Symbol(y, Decl(keyofInferenceIntersectsResults.ts, 6, 35)) +>T : Symbol(T, Decl(keyofInferenceIntersectsResults.ts, 6, 21)) +>T : Symbol(T, Decl(keyofInferenceIntersectsResults.ts, 6, 21)) + +const a = foo('a', 'b'); // compiles cleanly +>a : Symbol(a, Decl(keyofInferenceIntersectsResults.ts, 8, 5)) +>foo : Symbol(foo, Decl(keyofInferenceIntersectsResults.ts, 3, 1)) +>X : Symbol(X, Decl(keyofInferenceIntersectsResults.ts, 0, 0)) + +const b = foo('a', 'b'); // also clean +>b : Symbol(b, Decl(keyofInferenceIntersectsResults.ts, 9, 5)) +>foo : Symbol(foo, Decl(keyofInferenceIntersectsResults.ts, 3, 1)) + +const c = bar('a', 'b'); // still clean +>c : Symbol(c, Decl(keyofInferenceIntersectsResults.ts, 10, 5)) +>bar : Symbol(bar, Decl(keyofInferenceIntersectsResults.ts, 5, 55)) + diff --git a/tests/baselines/reference/keyofInferenceIntersectsResults.types b/tests/baselines/reference/keyofInferenceIntersectsResults.types new file mode 100644 index 0000000000000..ff7c3da19a685 --- /dev/null +++ b/tests/baselines/reference/keyofInferenceIntersectsResults.types @@ -0,0 +1,52 @@ +=== tests/cases/conformance/types/typeRelationships/typeInference/keyofInferenceIntersectsResults.ts === +interface X { +>X : X + + a: string; +>a : string + + b: string; +>b : string +} + +declare function foo(x: keyof T, y: keyof T): T; +>foo : (x: keyof T, y: keyof T) => T +>T : T +>X : X +>x : keyof T +>T : T +>y : keyof T +>T : T +>T : T + +declare function bar(x: keyof T, y: keyof T): T; +>bar : (x: keyof T, y: keyof T) => T +>T : T +>x : keyof T +>T : T +>y : keyof T +>T : T +>T : T + +const a = foo('a', 'b'); // compiles cleanly +>a : X +>foo('a', 'b') : X +>foo : (x: keyof T, y: keyof T) => T +>X : X +>'a' : "a" +>'b' : "b" + +const b = foo('a', 'b'); // also clean +>b : { a: any; } & { b: any; } +>foo('a', 'b') : { a: any; } & { b: any; } +>foo : (x: keyof T, y: keyof T) => T +>'a' : "a" +>'b' : "b" + +const c = bar('a', 'b'); // still clean +>c : { a: any; } & { b: any; } +>bar('a', 'b') : { a: any; } & { b: any; } +>bar : (x: keyof T, y: keyof T) => T +>'a' : "a" +>'b' : "b" + diff --git a/tests/baselines/reference/keyofInferenceLowerPriorityThanReturn.js b/tests/baselines/reference/keyofInferenceLowerPriorityThanReturn.js new file mode 100644 index 0000000000000..581e01729d9d7 --- /dev/null +++ b/tests/baselines/reference/keyofInferenceLowerPriorityThanReturn.js @@ -0,0 +1,57 @@ +//// [keyofInferenceLowerPriorityThanReturn.ts] +// #22736 +declare class Write { + protected dummy: Write; +} + +declare class Col { + protected dummy: [Col, s, a]; +} + +declare class Table { + protected dummy: [Table, Req, Def]; +} + +type MakeTable = { + [P in keyof T1]: Col; +} & { + [P in keyof T2]: Col; + }; + +declare class ConflictTarget { + public static tableColumns(cols: (keyof Cols)[]): ConflictTarget; + protected dummy: [ConflictTarget, Cols]; +} + + + +const bookTable: Table = null as any + +interface BookReq { + readonly title: string; + readonly serial: number; +} + +interface BookDef { + readonly author: string; + readonly numPages: number | null; +} + + +function insertOnConflictDoNothing(_table: Table, _conflictTarget: ConflictTarget): boolean { + throw new Error(); +} + +function f() { + insertOnConflictDoNothing(bookTable, ConflictTarget.tableColumns(["serial"])); // <-- No error here; should use the type inferred for the return type of `tableColumns` +} + + +//// [keyofInferenceLowerPriorityThanReturn.js] +var bookTable = null; +function insertOnConflictDoNothing(_table, _conflictTarget) { + throw new Error(); +} +function f() { + insertOnConflictDoNothing(bookTable, ConflictTarget.tableColumns(["serial"])); // <-- No error here; should use the type inferred for the return type of `tableColumns` +} diff --git a/tests/baselines/reference/keyofInferenceLowerPriorityThanReturn.symbols b/tests/baselines/reference/keyofInferenceLowerPriorityThanReturn.symbols new file mode 100644 index 0000000000000..c4c6b0c3cc33c --- /dev/null +++ b/tests/baselines/reference/keyofInferenceLowerPriorityThanReturn.symbols @@ -0,0 +1,138 @@ +=== tests/cases/conformance/types/typeRelationships/typeInference/keyofInferenceLowerPriorityThanReturn.ts === +// #22736 +declare class Write { +>Write : Symbol(Write, Decl(keyofInferenceLowerPriorityThanReturn.ts, 0, 0)) + + protected dummy: Write; +>dummy : Symbol(Write.dummy, Decl(keyofInferenceLowerPriorityThanReturn.ts, 1, 21)) +>Write : Symbol(Write, Decl(keyofInferenceLowerPriorityThanReturn.ts, 0, 0)) +} + +declare class Col { +>Col : Symbol(Col, Decl(keyofInferenceLowerPriorityThanReturn.ts, 3, 1)) +>s : Symbol(s, Decl(keyofInferenceLowerPriorityThanReturn.ts, 5, 18)) +>a : Symbol(a, Decl(keyofInferenceLowerPriorityThanReturn.ts, 5, 20)) + + protected dummy: [Col, s, a]; +>dummy : Symbol(Col.dummy, Decl(keyofInferenceLowerPriorityThanReturn.ts, 5, 25)) +>Col : Symbol(Col, Decl(keyofInferenceLowerPriorityThanReturn.ts, 3, 1)) +>s : Symbol(s, Decl(keyofInferenceLowerPriorityThanReturn.ts, 5, 18)) +>a : Symbol(a, Decl(keyofInferenceLowerPriorityThanReturn.ts, 5, 20)) +>s : Symbol(s, Decl(keyofInferenceLowerPriorityThanReturn.ts, 5, 18)) +>a : Symbol(a, Decl(keyofInferenceLowerPriorityThanReturn.ts, 5, 20)) +} + +declare class Table { +>Table : Symbol(Table, Decl(keyofInferenceLowerPriorityThanReturn.ts, 7, 1)) +>Req : Symbol(Req, Decl(keyofInferenceLowerPriorityThanReturn.ts, 9, 20)) +>Def : Symbol(Def, Decl(keyofInferenceLowerPriorityThanReturn.ts, 9, 24)) + + protected dummy: [Table, Req, Def]; +>dummy : Symbol(Table.dummy, Decl(keyofInferenceLowerPriorityThanReturn.ts, 9, 31)) +>Table : Symbol(Table, Decl(keyofInferenceLowerPriorityThanReturn.ts, 7, 1)) +>Req : Symbol(Req, Decl(keyofInferenceLowerPriorityThanReturn.ts, 9, 20)) +>Def : Symbol(Def, Decl(keyofInferenceLowerPriorityThanReturn.ts, 9, 24)) +>Req : Symbol(Req, Decl(keyofInferenceLowerPriorityThanReturn.ts, 9, 20)) +>Def : Symbol(Def, Decl(keyofInferenceLowerPriorityThanReturn.ts, 9, 24)) +} + +type MakeTable = { +>MakeTable : Symbol(MakeTable, Decl(keyofInferenceLowerPriorityThanReturn.ts, 11, 1)) +>T1 : Symbol(T1, Decl(keyofInferenceLowerPriorityThanReturn.ts, 13, 15)) +>T2 : Symbol(T2, Decl(keyofInferenceLowerPriorityThanReturn.ts, 13, 33)) + + [P in keyof T1]: Col; +>P : Symbol(P, Decl(keyofInferenceLowerPriorityThanReturn.ts, 14, 5)) +>T1 : Symbol(T1, Decl(keyofInferenceLowerPriorityThanReturn.ts, 13, 15)) +>Col : Symbol(Col, Decl(keyofInferenceLowerPriorityThanReturn.ts, 3, 1)) +>Write : Symbol(Write, Decl(keyofInferenceLowerPriorityThanReturn.ts, 0, 0)) +>T1 : Symbol(T1, Decl(keyofInferenceLowerPriorityThanReturn.ts, 13, 15)) +>P : Symbol(P, Decl(keyofInferenceLowerPriorityThanReturn.ts, 14, 5)) + +} & { + [P in keyof T2]: Col; +>P : Symbol(P, Decl(keyofInferenceLowerPriorityThanReturn.ts, 16, 9)) +>T2 : Symbol(T2, Decl(keyofInferenceLowerPriorityThanReturn.ts, 13, 33)) +>Col : Symbol(Col, Decl(keyofInferenceLowerPriorityThanReturn.ts, 3, 1)) +>Write : Symbol(Write, Decl(keyofInferenceLowerPriorityThanReturn.ts, 0, 0)) +>T2 : Symbol(T2, Decl(keyofInferenceLowerPriorityThanReturn.ts, 13, 33)) +>P : Symbol(P, Decl(keyofInferenceLowerPriorityThanReturn.ts, 16, 9)) + + }; + +declare class ConflictTarget { +>ConflictTarget : Symbol(ConflictTarget, Decl(keyofInferenceLowerPriorityThanReturn.ts, 17, 6)) +>Cols : Symbol(Cols, Decl(keyofInferenceLowerPriorityThanReturn.ts, 19, 29)) + + public static tableColumns(cols: (keyof Cols)[]): ConflictTarget; +>tableColumns : Symbol(ConflictTarget.tableColumns, Decl(keyofInferenceLowerPriorityThanReturn.ts, 19, 36)) +>Cols : Symbol(Cols, Decl(keyofInferenceLowerPriorityThanReturn.ts, 20, 31)) +>cols : Symbol(cols, Decl(keyofInferenceLowerPriorityThanReturn.ts, 20, 37)) +>Cols : Symbol(Cols, Decl(keyofInferenceLowerPriorityThanReturn.ts, 20, 31)) +>ConflictTarget : Symbol(ConflictTarget, Decl(keyofInferenceLowerPriorityThanReturn.ts, 17, 6)) +>Cols : Symbol(Cols, Decl(keyofInferenceLowerPriorityThanReturn.ts, 20, 31)) + + protected dummy: [ConflictTarget, Cols]; +>dummy : Symbol(ConflictTarget.dummy, Decl(keyofInferenceLowerPriorityThanReturn.ts, 20, 81)) +>ConflictTarget : Symbol(ConflictTarget, Decl(keyofInferenceLowerPriorityThanReturn.ts, 17, 6)) +>Cols : Symbol(Cols, Decl(keyofInferenceLowerPriorityThanReturn.ts, 19, 29)) +>Cols : Symbol(Cols, Decl(keyofInferenceLowerPriorityThanReturn.ts, 19, 29)) +} + + + +const bookTable: Table = null as any +>bookTable : Symbol(bookTable, Decl(keyofInferenceLowerPriorityThanReturn.ts, 26, 5)) +>Table : Symbol(Table, Decl(keyofInferenceLowerPriorityThanReturn.ts, 7, 1)) +>BookReq : Symbol(BookReq, Decl(keyofInferenceLowerPriorityThanReturn.ts, 26, 54)) +>BookDef : Symbol(BookDef, Decl(keyofInferenceLowerPriorityThanReturn.ts, 31, 1)) + +interface BookReq { +>BookReq : Symbol(BookReq, Decl(keyofInferenceLowerPriorityThanReturn.ts, 26, 54)) + + readonly title: string; +>title : Symbol(BookReq.title, Decl(keyofInferenceLowerPriorityThanReturn.ts, 28, 19)) + + readonly serial: number; +>serial : Symbol(BookReq.serial, Decl(keyofInferenceLowerPriorityThanReturn.ts, 29, 27)) +} + +interface BookDef { +>BookDef : Symbol(BookDef, Decl(keyofInferenceLowerPriorityThanReturn.ts, 31, 1)) + + readonly author: string; +>author : Symbol(BookDef.author, Decl(keyofInferenceLowerPriorityThanReturn.ts, 33, 19)) + + readonly numPages: number | null; +>numPages : Symbol(BookDef.numPages, Decl(keyofInferenceLowerPriorityThanReturn.ts, 34, 28)) +} + + +function insertOnConflictDoNothing(_table: Table, _conflictTarget: ConflictTarget): boolean { +>insertOnConflictDoNothing : Symbol(insertOnConflictDoNothing, Decl(keyofInferenceLowerPriorityThanReturn.ts, 36, 1)) +>Req : Symbol(Req, Decl(keyofInferenceLowerPriorityThanReturn.ts, 39, 35)) +>Def : Symbol(Def, Decl(keyofInferenceLowerPriorityThanReturn.ts, 39, 54)) +>_table : Symbol(_table, Decl(keyofInferenceLowerPriorityThanReturn.ts, 39, 75)) +>Table : Symbol(Table, Decl(keyofInferenceLowerPriorityThanReturn.ts, 7, 1)) +>Req : Symbol(Req, Decl(keyofInferenceLowerPriorityThanReturn.ts, 39, 35)) +>Def : Symbol(Def, Decl(keyofInferenceLowerPriorityThanReturn.ts, 39, 54)) +>_conflictTarget : Symbol(_conflictTarget, Decl(keyofInferenceLowerPriorityThanReturn.ts, 39, 99)) +>ConflictTarget : Symbol(ConflictTarget, Decl(keyofInferenceLowerPriorityThanReturn.ts, 17, 6)) +>Req : Symbol(Req, Decl(keyofInferenceLowerPriorityThanReturn.ts, 39, 35)) +>Def : Symbol(Def, Decl(keyofInferenceLowerPriorityThanReturn.ts, 39, 54)) + + throw new Error(); +>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +} + +function f() { +>f : Symbol(f, Decl(keyofInferenceLowerPriorityThanReturn.ts, 41, 1)) + + insertOnConflictDoNothing(bookTable, ConflictTarget.tableColumns(["serial"])); // <-- No error here; should use the type inferred for the return type of `tableColumns` +>insertOnConflictDoNothing : Symbol(insertOnConflictDoNothing, Decl(keyofInferenceLowerPriorityThanReturn.ts, 36, 1)) +>bookTable : Symbol(bookTable, Decl(keyofInferenceLowerPriorityThanReturn.ts, 26, 5)) +>ConflictTarget.tableColumns : Symbol(ConflictTarget.tableColumns, Decl(keyofInferenceLowerPriorityThanReturn.ts, 19, 36)) +>ConflictTarget : Symbol(ConflictTarget, Decl(keyofInferenceLowerPriorityThanReturn.ts, 17, 6)) +>tableColumns : Symbol(ConflictTarget.tableColumns, Decl(keyofInferenceLowerPriorityThanReturn.ts, 19, 36)) +} + diff --git a/tests/baselines/reference/keyofInferenceLowerPriorityThanReturn.types b/tests/baselines/reference/keyofInferenceLowerPriorityThanReturn.types new file mode 100644 index 0000000000000..cf0c3cb6a319b --- /dev/null +++ b/tests/baselines/reference/keyofInferenceLowerPriorityThanReturn.types @@ -0,0 +1,146 @@ +=== tests/cases/conformance/types/typeRelationships/typeInference/keyofInferenceLowerPriorityThanReturn.ts === +// #22736 +declare class Write { +>Write : Write + + protected dummy: Write; +>dummy : Write +>Write : Write +} + +declare class Col { +>Col : Col +>s : s +>a : a + + protected dummy: [Col, s, a]; +>dummy : [Col, s, a] +>Col : Col +>s : s +>a : a +>s : s +>a : a +} + +declare class Table { +>Table : Table +>Req : Req +>Def : Def + + protected dummy: [Table, Req, Def]; +>dummy : [Table, Req, Def] +>Table : Table +>Req : Req +>Def : Def +>Req : Req +>Def : Def +} + +type MakeTable = { +>MakeTable : MakeTable +>T1 : T1 +>T2 : T2 + + [P in keyof T1]: Col; +>P : P +>T1 : T1 +>Col : Col +>Write : Write +>T1 : T1 +>P : P + +} & { + [P in keyof T2]: Col; +>P : P +>T2 : T2 +>Col : Col +>Write : Write +>T2 : T2 +>P : P + + }; + +declare class ConflictTarget { +>ConflictTarget : ConflictTarget +>Cols : Cols + + public static tableColumns(cols: (keyof Cols)[]): ConflictTarget; +>tableColumns : (cols: (keyof Cols)[]) => ConflictTarget +>Cols : Cols +>cols : (keyof Cols)[] +>Cols : Cols +>ConflictTarget : ConflictTarget +>Cols : Cols + + protected dummy: [ConflictTarget, Cols]; +>dummy : [ConflictTarget, Cols] +>ConflictTarget : ConflictTarget +>Cols : Cols +>Cols : Cols +} + + + +const bookTable: Table = null as any +>bookTable : Table +>Table : Table +>BookReq : BookReq +>BookDef : BookDef +>null as any : any +>null : null + +interface BookReq { +>BookReq : BookReq + + readonly title: string; +>title : string + + readonly serial: number; +>serial : number +} + +interface BookDef { +>BookDef : BookDef + + readonly author: string; +>author : string + + readonly numPages: number | null; +>numPages : number +>null : null +} + + +function insertOnConflictDoNothing(_table: Table, _conflictTarget: ConflictTarget): boolean { +>insertOnConflictDoNothing : (_table: Table, _conflictTarget: ConflictTarget) => boolean +>Req : Req +>Def : Def +>_table : Table +>Table : Table +>Req : Req +>Def : Def +>_conflictTarget : ConflictTarget +>ConflictTarget : ConflictTarget +>Req : Req +>Def : Def + + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor +} + +function f() { +>f : () => void + + insertOnConflictDoNothing(bookTable, ConflictTarget.tableColumns(["serial"])); // <-- No error here; should use the type inferred for the return type of `tableColumns` +>insertOnConflictDoNothing(bookTable, ConflictTarget.tableColumns(["serial"])) : boolean +>insertOnConflictDoNothing : (_table: Table, _conflictTarget: ConflictTarget) => boolean +>bookTable : Table +>ConflictTarget.tableColumns(["serial"]) : ConflictTarget +>ConflictTarget.tableColumns : (cols: (keyof Cols)[]) => ConflictTarget +>ConflictTarget : typeof ConflictTarget +>tableColumns : (cols: (keyof Cols)[]) => ConflictTarget +>["serial"] : "serial"[] +>"serial" : "serial" +} + diff --git a/tests/cases/conformance/types/typeRelationships/typeInference/keyofInferenceIntersectsResults.ts b/tests/cases/conformance/types/typeRelationships/typeInference/keyofInferenceIntersectsResults.ts new file mode 100644 index 0000000000000..7bd11ed2ae39e --- /dev/null +++ b/tests/cases/conformance/types/typeRelationships/typeInference/keyofInferenceIntersectsResults.ts @@ -0,0 +1,11 @@ +interface X { + a: string; + b: string; +} + +declare function foo(x: keyof T, y: keyof T): T; +declare function bar(x: keyof T, y: keyof T): T; + +const a = foo('a', 'b'); // compiles cleanly +const b = foo('a', 'b'); // also clean +const c = bar('a', 'b'); // still clean \ No newline at end of file diff --git a/tests/cases/conformance/types/typeRelationships/typeInference/keyofInferenceLowerPriorityThanReturn.ts b/tests/cases/conformance/types/typeRelationships/typeInference/keyofInferenceLowerPriorityThanReturn.ts new file mode 100644 index 0000000000000..9de110bd1e95e --- /dev/null +++ b/tests/cases/conformance/types/typeRelationships/typeInference/keyofInferenceLowerPriorityThanReturn.ts @@ -0,0 +1,46 @@ +// #22736 +declare class Write { + protected dummy: Write; +} + +declare class Col { + protected dummy: [Col, s, a]; +} + +declare class Table { + protected dummy: [Table, Req, Def]; +} + +type MakeTable = { + [P in keyof T1]: Col; +} & { + [P in keyof T2]: Col; + }; + +declare class ConflictTarget { + public static tableColumns(cols: (keyof Cols)[]): ConflictTarget; + protected dummy: [ConflictTarget, Cols]; +} + + + +const bookTable: Table = null as any + +interface BookReq { + readonly title: string; + readonly serial: number; +} + +interface BookDef { + readonly author: string; + readonly numPages: number | null; +} + + +function insertOnConflictDoNothing(_table: Table, _conflictTarget: ConflictTarget): boolean { + throw new Error(); +} + +function f() { + insertOnConflictDoNothing(bookTable, ConflictTarget.tableColumns(["serial"])); // <-- No error here; should use the type inferred for the return type of `tableColumns` +}