Use strictSubtypeRelation only as tiebreaker

This commit is contained in:
Anders Hejlsberg
2020-12-18 12:12:28 -10:00
parent e07f199d4c
commit 581048b97a
8 changed files with 27 additions and 24 deletions
+2 -2
View File
@@ -13154,7 +13154,7 @@ namespace ts {
}
}
count++;
if (isTypeRelatedTo(source, target, strictSubtypeRelation) && (
if (isTypeSubtypeOf(source, target) && (!isTypeSubtypeOf(target, source) || isTypeRelatedTo(source, target, strictSubtypeRelation)) && (
!(getObjectFlags(getTargetType(source)) & ObjectFlags.Class) ||
!(getObjectFlags(getTargetType(target)) & ObjectFlags.Class) ||
isTypeDerivedFrom(source, target))) {
@@ -16231,7 +16231,7 @@ namespace ts {
return Ternary.False;
}
const strictVariance = !(checkMode & SignatureCheckMode.Callback) && strictFunctionTypes && (checkMode & SignatureCheckMode.StrictArity || isStrictSignature(target));
const strictVariance = !(checkMode & SignatureCheckMode.Callback) && (checkMode & SignatureCheckMode.StrictArity || strictFunctionTypes && isStrictSignature(target));
let result = Ternary.True;
const sourceThisType = getThisTypeOfSignature(source);
@@ -36,8 +36,8 @@ var cs = [a, b, c]; // { x: number; y?: number };[]
>c : { x: number; a?: number; }
var ds = [(x: Object) => 1, (x: string) => 2]; // { (x:Object) => number }[]
>ds : ((x: Object) => number)[]
>[(x: Object) => 1, (x: string) => 2] : ((x: Object) => number)[]
>ds : ((x: string) => number)[]
>[(x: Object) => 1, (x: string) => 2] : ((x: string) => number)[]
>(x: Object) => 1 : (x: Object) => number
>x : Object
>1 : 1
@@ -23,8 +23,8 @@ class C extends A {
}
var xs = [(x: A) => { }, (x: B) => { }, (x: C) => { }];
>xs : ((x: A) => void)[]
>[(x: A) => { }, (x: B) => { }, (x: C) => { }] : ((x: A) => void)[]
>xs : (((x: B) => void) | ((x: C) => void))[]
>[(x: A) => { }, (x: B) => { }, (x: C) => { }] : (((x: B) => void) | ((x: C) => void))[]
>(x: A) => { } : (x: A) => void
>x : A
>(x: B) => { } : (x: B) => void
@@ -58,7 +58,7 @@ function f4() {
}
function f5() {
>f5 : () => Object | 1
>f5 : () => Object
return 1;
>1 : 1
@@ -566,16 +566,16 @@ function f20<T extends Number>(x: T) {
>x : T
var r19 = true ? new Object() : x; // ok
>r19 : Object | T
>true ? new Object() : x : Object | T
>r19 : Object
>true ? new Object() : x : Object
>true : true
>new Object() : Object
>Object : ObjectConstructor
>x : T
var r19 = true ? x : new Object(); // ok
>r19 : Object | T
>true ? x : new Object() : Object | T
>r19 : Object
>true ? x : new Object() : Object
>true : true
>x : T
>new Object() : Object
@@ -1,7 +1,8 @@
tests/cases/conformance/types/union/unionTypeReduction2.ts(33,5): error TS2554: Expected 1 arguments, but got 0.
tests/cases/conformance/types/union/unionTypeReduction2.ts(49,9): error TS2554: Expected 0 arguments, but got 1.
==== tests/cases/conformance/types/union/unionTypeReduction2.ts (1 errors) ====
==== tests/cases/conformance/types/union/unionTypeReduction2.ts (2 errors) ====
function f1(x: { f(): void }, y: { f(x?: string): void }) {
let z = !!true ? x : y; // { f(x?: string): void }
z.f();
@@ -54,6 +55,8 @@ tests/cases/conformance/types/union/unionTypeReduction2.ts(33,5): error TS2554:
let z = !!true ? a : b; // A | B
z.f();
z.f('hello');
~~~~~~~
!!! error TS2554: Expected 0 arguments, but got 1.
}
// Repro from #35414
@@ -154,14 +154,14 @@ function f11(a: A, b: B) {
>b : Symbol(b, Decl(unionTypeReduction2.ts, 45, 18))
z.f();
>z.f : Symbol(f, Decl(unionTypeReduction2.ts, 36, 10), Decl(unionTypeReduction2.ts, 40, 10))
>z.f : Symbol(f, Decl(unionTypeReduction2.ts, 36, 10))
>z : Symbol(z, Decl(unionTypeReduction2.ts, 46, 7))
>f : Symbol(f, Decl(unionTypeReduction2.ts, 36, 10), Decl(unionTypeReduction2.ts, 40, 10))
>f : Symbol(f, Decl(unionTypeReduction2.ts, 36, 10))
z.f('hello');
>z.f : Symbol(f, Decl(unionTypeReduction2.ts, 36, 10), Decl(unionTypeReduction2.ts, 40, 10))
>z.f : Symbol(f, Decl(unionTypeReduction2.ts, 36, 10))
>z : Symbol(z, Decl(unionTypeReduction2.ts, 46, 7))
>f : Symbol(f, Decl(unionTypeReduction2.ts, 36, 10), Decl(unionTypeReduction2.ts, 40, 10))
>f : Symbol(f, Decl(unionTypeReduction2.ts, 36, 10))
}
// Repro from #35414
@@ -189,8 +189,8 @@ function f11(a: A, b: B) {
>b : B
let z = !!true ? a : b; // A | B
>z : A | B
>!!true ? a : b : A | B
>z : A
>!!true ? a : b : A
>!!true : true
>!true : false
>true : true
@@ -199,15 +199,15 @@ function f11(a: A, b: B) {
z.f();
>z.f() : void
>z.f : ((x?: string | undefined) => void) | (() => void)
>z : A | B
>f : ((x?: string | undefined) => void) | (() => void)
>z.f : () => void
>z : A
>f : () => void
z.f('hello');
>z.f('hello') : void
>z.f : ((x?: string | undefined) => void) | (() => void)
>z : A | B
>f : ((x?: string | undefined) => void) | (() => void)
>z.f : () => void
>z : A
>f : () => void
>'hello' : "hello"
}