Skip to content

Fix reverse mapped contravariant inference #21292

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
merged 3 commits into from
Jan 19, 2018
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: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11029,7 +11029,9 @@ namespace ts {
const templateType = getTemplateTypeFromMappedType(target);
const inference = createInferenceInfo(typeParameter);
inferTypes([inference], sourceType, templateType);
return inference.candidates ? getUnionType(inference.candidates, UnionReduction.Subtype) : emptyObjectType;
return inference.candidates ? getUnionType(inference.candidates, UnionReduction.Subtype) :
inference.contraCandidates ? getCommonSubtype(inference.contraCandidates) :
emptyObjectType;
}

function getUnmatchedProperty(source: Type, target: Type, requireOptionalProperties: boolean) {
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/reverseMappedContravariantInference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [reverseMappedContravariantInference.ts]
// Repro from #21273

declare function conforms<T>(source: { [K in keyof T]: (val: T[K]) => boolean }): (value: T) => boolean;
conforms({ foo: (v: string) => false })({ foo: "hello" });


//// [reverseMappedContravariantInference.js]
"use strict";
// Repro from #21273
conforms({ foo: function (v) { return false; } })({ foo: "hello" });
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/reverseMappedContravariantInference.ts ===
// Repro from #21273

declare function conforms<T>(source: { [K in keyof T]: (val: T[K]) => boolean }): (value: T) => boolean;
>conforms : Symbol(conforms, Decl(reverseMappedContravariantInference.ts, 0, 0))
>T : Symbol(T, Decl(reverseMappedContravariantInference.ts, 2, 26))
>source : Symbol(source, Decl(reverseMappedContravariantInference.ts, 2, 29))
>K : Symbol(K, Decl(reverseMappedContravariantInference.ts, 2, 40))
>T : Symbol(T, Decl(reverseMappedContravariantInference.ts, 2, 26))
>val : Symbol(val, Decl(reverseMappedContravariantInference.ts, 2, 56))
>T : Symbol(T, Decl(reverseMappedContravariantInference.ts, 2, 26))
>K : Symbol(K, Decl(reverseMappedContravariantInference.ts, 2, 40))
>value : Symbol(value, Decl(reverseMappedContravariantInference.ts, 2, 83))
>T : Symbol(T, Decl(reverseMappedContravariantInference.ts, 2, 26))

conforms({ foo: (v: string) => false })({ foo: "hello" });
>conforms : Symbol(conforms, Decl(reverseMappedContravariantInference.ts, 0, 0))
>foo : Symbol(foo, Decl(reverseMappedContravariantInference.ts, 3, 10))
>v : Symbol(v, Decl(reverseMappedContravariantInference.ts, 3, 17))
>foo : Symbol(foo, Decl(reverseMappedContravariantInference.ts, 3, 41))

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/compiler/reverseMappedContravariantInference.ts ===
// Repro from #21273

declare function conforms<T>(source: { [K in keyof T]: (val: T[K]) => boolean }): (value: T) => boolean;
>conforms : <T>(source: { [K in keyof T]: (val: T[K]) => boolean; }) => (value: T) => boolean
>T : T
>source : { [K in keyof T]: (val: T[K]) => boolean; }
>K : K
>T : T
>val : T[K]
>T : T
>K : K
>value : T
>T : T

conforms({ foo: (v: string) => false })({ foo: "hello" });
>conforms({ foo: (v: string) => false })({ foo: "hello" }) : boolean
>conforms({ foo: (v: string) => false }) : (value: { foo: any; }) => boolean
>conforms : <T>(source: { [K in keyof T]: (val: T[K]) => boolean; }) => (value: T) => boolean
>{ foo: (v: string) => false } : { foo: (v: string) => boolean; }
>foo : (v: string) => boolean
>(v: string) => false : (v: string) => boolean
>v : string
>false : false
>{ foo: "hello" } : { foo: string; }
>foo : string
>"hello" : "hello"

6 changes: 6 additions & 0 deletions tests/cases/compiler/reverseMappedContravariantInference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @strict: true

// Repro from #21273

declare function conforms<T>(source: { [K in keyof T]: (val: T[K]) => boolean }): (value: T) => boolean;
conforms({ foo: (v: string) => false })({ foo: "hello" });