diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 780b120bb7e5f..358c27f5145ad 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -19053,7 +19053,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { if (newConstraint.flags & TypeFlags.AnyOrUnknown || isTypeAssignableTo(getRestrictiveInstantiation(newBaseType), getRestrictiveInstantiation(newConstraint))) { return newBaseType; } - return newBaseType.flags & TypeFlags.TypeVariable ? getSubstitutionType(newBaseType, newConstraint) : getIntersectionType([newConstraint, newBaseType]); + return (newBaseType.flags & TypeFlags.TypeVariable || newConstraint.flags & TypeFlags.TypeVariable) ? getSubstitutionType(newBaseType, newConstraint) : getIntersectionType([newConstraint, newBaseType]); } return type; } diff --git a/tests/baselines/reference/callOfConditionalTypeWithConcreteBranches.types b/tests/baselines/reference/callOfConditionalTypeWithConcreteBranches.types index 412986f9e27e0..9e3a5bf72a126 100644 --- a/tests/baselines/reference/callOfConditionalTypeWithConcreteBranches.types +++ b/tests/baselines/reference/callOfConditionalTypeWithConcreteBranches.types @@ -64,11 +64,11 @@ function fn2(arg: Q2) { arg(arg => useT(arg)); >arg(arg => useT(arg)) : void >arg : Q2 ->arg => useT(arg) : (arg: T & number) => void ->arg : T & number +>arg => useT(arg) : (arg: number) => void +>arg : number >useT(arg) : void >useT : (_arg: T) => void ->arg : T & number +>arg : number } // Legal invocations are not problematic fn2(m => m(42)); diff --git a/tests/baselines/reference/conditionalSubstitutionInferencesLowerPriority.js b/tests/baselines/reference/conditionalSubstitutionInferencesLowerPriority.js new file mode 100644 index 0000000000000..ab3238a13daf0 --- /dev/null +++ b/tests/baselines/reference/conditionalSubstitutionInferencesLowerPriority.js @@ -0,0 +1,23 @@ +//// [conditionalSubstitutionInferencesLowerPriority.ts] +type TestType = string extends Keys ? Record : Record + +function inferHelper(data: TestType) { + return data; +} + +export const a = inferHelper({ + // key1 is inferred to be value1 here + key1: "value1" +}) + +//// [conditionalSubstitutionInferencesLowerPriority.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.a = void 0; +function inferHelper(data) { + return data; +} +exports.a = inferHelper({ + // key1 is inferred to be value1 here + key1: "value1" +}); diff --git a/tests/baselines/reference/conditionalSubstitutionInferencesLowerPriority.symbols b/tests/baselines/reference/conditionalSubstitutionInferencesLowerPriority.symbols new file mode 100644 index 0000000000000..d75c627a143ee --- /dev/null +++ b/tests/baselines/reference/conditionalSubstitutionInferencesLowerPriority.symbols @@ -0,0 +1,30 @@ +=== tests/cases/compiler/conditionalSubstitutionInferencesLowerPriority.ts === +type TestType = string extends Keys ? Record : Record +>TestType : Symbol(TestType, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 0, 0)) +>Keys : Symbol(Keys, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 0, 14)) +>Keys : Symbol(Keys, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 0, 14)) +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) +>Keys : Symbol(Keys, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 0, 14)) +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) +>Keys : Symbol(Keys, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 0, 14)) + +function inferHelper(data: TestType) { +>inferHelper : Symbol(inferHelper, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 0, 102)) +>Keys : Symbol(Keys, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 2, 21)) +>data : Symbol(data, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 2, 42)) +>TestType : Symbol(TestType, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 0, 0)) +>Keys : Symbol(Keys, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 2, 21)) + + return data; +>data : Symbol(data, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 2, 42)) +} + +export const a = inferHelper({ +>a : Symbol(a, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 6, 12)) +>inferHelper : Symbol(inferHelper, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 0, 102)) + + // key1 is inferred to be value1 here + key1: "value1" +>key1 : Symbol(key1, Decl(conditionalSubstitutionInferencesLowerPriority.ts, 6, 30)) + +}) diff --git a/tests/baselines/reference/conditionalSubstitutionInferencesLowerPriority.types b/tests/baselines/reference/conditionalSubstitutionInferencesLowerPriority.types new file mode 100644 index 0000000000000..17e31f6bc978e --- /dev/null +++ b/tests/baselines/reference/conditionalSubstitutionInferencesLowerPriority.types @@ -0,0 +1,24 @@ +=== tests/cases/compiler/conditionalSubstitutionInferencesLowerPriority.ts === +type TestType = string extends Keys ? Record : Record +>TestType : TestType + +function inferHelper(data: TestType) { +>inferHelper : (data: TestType) => TestType +>data : TestType + + return data; +>data : TestType +} + +export const a = inferHelper({ +>a : Record<"key1", string> +>inferHelper({ // key1 is inferred to be value1 here key1: "value1"}) : Record<"key1", string> +>inferHelper : (data: TestType) => TestType +>{ // key1 is inferred to be value1 here key1: "value1"} : { key1: string; } + + // key1 is inferred to be value1 here + key1: "value1" +>key1 : string +>"value1" : "value1" + +}) diff --git a/tests/cases/compiler/conditionalSubstitutionInferencesLowerPriority.ts b/tests/cases/compiler/conditionalSubstitutionInferencesLowerPriority.ts new file mode 100644 index 0000000000000..e53568ad43585 --- /dev/null +++ b/tests/cases/compiler/conditionalSubstitutionInferencesLowerPriority.ts @@ -0,0 +1,10 @@ +type TestType = string extends Keys ? Record : Record + +function inferHelper(data: TestType) { + return data; +} + +export const a = inferHelper({ + // key1 is inferred to be value1 here + key1: "value1" +}) \ No newline at end of file