mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Merge pull request #28672 from Microsoft/overlappy-3.2
[release-3.2] Overlappy elaboration on unions
This commit is contained in:
+30
-1
@@ -11921,7 +11921,8 @@ namespace ts {
|
||||
findMatchingDiscriminantType(source, target) ||
|
||||
findMatchingTypeReferenceOrTypeAliasReference(source, target) ||
|
||||
findBestTypeForObjectLiteral(source, target) ||
|
||||
findBestTypeForInvokable(source, target);
|
||||
findBestTypeForInvokable(source, target) ||
|
||||
findMostOverlappyType(source, target);
|
||||
|
||||
isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true);
|
||||
}
|
||||
@@ -11961,6 +11962,34 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
function findMostOverlappyType(source: Type, unionTarget: UnionOrIntersectionType) {
|
||||
let bestMatch: Type | undefined;
|
||||
let matchingCount = 0;
|
||||
for (const target of unionTarget.types) {
|
||||
const overlap = getIntersectionType([getIndexType(source), getIndexType(target)]);
|
||||
if (overlap.flags & TypeFlags.Index) {
|
||||
// perfect overlap of keys
|
||||
bestMatch = target;
|
||||
matchingCount = Infinity;
|
||||
}
|
||||
else if (overlap.flags & TypeFlags.Union) {
|
||||
// Some subset overlap if we have only string literals.
|
||||
// If we have a union of index types, it seems likely that we
|
||||
// needed to elaborate between two generic mapped types anyway.
|
||||
const len = length((overlap as UnionType).types);
|
||||
if (len >= matchingCount) {
|
||||
bestMatch = target;
|
||||
matchingCount = len;
|
||||
}
|
||||
}
|
||||
else if (!(overlap.flags & TypeFlags.Never) && 1 >= matchingCount) {
|
||||
bestMatch = target;
|
||||
matchingCount = 1;
|
||||
}
|
||||
}
|
||||
return bestMatch;
|
||||
}
|
||||
|
||||
// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly
|
||||
function findMatchingDiscriminantType(source: Type, target: Type) {
|
||||
if (target.flags & TypeFlags.Union) {
|
||||
|
||||
@@ -3,19 +3,19 @@ tests/cases/conformance/jsx/file.tsx(17,11): error TS2710: 'children' are specif
|
||||
tests/cases/conformance/jsx/file.tsx(31,6): error TS2322: Type '{ children: (Element | ((name: string) => Element))[]; a: number; b: string; }' is not assignable to type 'Prop'.
|
||||
Types of property 'children' are incompatible.
|
||||
Type '(Element | ((name: string) => Element))[]' is not assignable to type 'string | Element'.
|
||||
Type '(Element | ((name: string) => Element))[]' is missing the following properties from type 'Element': type, props
|
||||
Type '(Element | ((name: string) => Element))[]' is not assignable to type 'string'.
|
||||
tests/cases/conformance/jsx/file.tsx(37,6): error TS2322: Type '{ children: (number | Element)[]; a: number; b: string; }' is not assignable to type 'Prop'.
|
||||
Types of property 'children' are incompatible.
|
||||
Type '(number | Element)[]' is not assignable to type 'string | Element'.
|
||||
Type '(number | Element)[]' is missing the following properties from type 'Element': type, props
|
||||
Type '(number | Element)[]' is not assignable to type 'string'.
|
||||
tests/cases/conformance/jsx/file.tsx(43,6): error TS2322: Type '{ children: (string | Element)[]; a: number; b: string; }' is not assignable to type 'Prop'.
|
||||
Types of property 'children' are incompatible.
|
||||
Type '(string | Element)[]' is not assignable to type 'string | Element'.
|
||||
Type '(string | Element)[]' is missing the following properties from type 'Element': type, props
|
||||
Type '(string | Element)[]' is not assignable to type 'string'.
|
||||
tests/cases/conformance/jsx/file.tsx(49,6): error TS2322: Type '{ children: Element[]; a: number; b: string; }' is not assignable to type 'Prop'.
|
||||
Types of property 'children' are incompatible.
|
||||
Type 'Element[]' is not assignable to type 'string | Element'.
|
||||
Type 'Element[]' is missing the following properties from type 'Element': type, props
|
||||
Type 'Element[]' is not assignable to type 'string'.
|
||||
|
||||
|
||||
==== tests/cases/conformance/jsx/file.tsx (6 errors) ====
|
||||
@@ -59,7 +59,7 @@ tests/cases/conformance/jsx/file.tsx(49,6): error TS2322: Type '{ children: Elem
|
||||
!!! error TS2322: Type '{ children: (Element | ((name: string) => Element))[]; a: number; b: string; }' is not assignable to type 'Prop'.
|
||||
!!! error TS2322: Types of property 'children' are incompatible.
|
||||
!!! error TS2322: Type '(Element | ((name: string) => Element))[]' is not assignable to type 'string | Element'.
|
||||
!!! error TS2322: Type '(Element | ((name: string) => Element))[]' is missing the following properties from type 'Element': type, props
|
||||
!!! error TS2322: Type '(Element | ((name: string) => Element))[]' is not assignable to type 'string'.
|
||||
<div> My Div </div>
|
||||
{(name: string) => <div> My name {name} </div>}
|
||||
</Comp>;
|
||||
@@ -70,7 +70,7 @@ tests/cases/conformance/jsx/file.tsx(49,6): error TS2322: Type '{ children: Elem
|
||||
!!! error TS2322: Type '{ children: (number | Element)[]; a: number; b: string; }' is not assignable to type 'Prop'.
|
||||
!!! error TS2322: Types of property 'children' are incompatible.
|
||||
!!! error TS2322: Type '(number | Element)[]' is not assignable to type 'string | Element'.
|
||||
!!! error TS2322: Type '(number | Element)[]' is missing the following properties from type 'Element': type, props
|
||||
!!! error TS2322: Type '(number | Element)[]' is not assignable to type 'string'.
|
||||
<div> My Div </div>
|
||||
{1000000}
|
||||
</Comp>;
|
||||
@@ -81,7 +81,7 @@ tests/cases/conformance/jsx/file.tsx(49,6): error TS2322: Type '{ children: Elem
|
||||
!!! error TS2322: Type '{ children: (string | Element)[]; a: number; b: string; }' is not assignable to type 'Prop'.
|
||||
!!! error TS2322: Types of property 'children' are incompatible.
|
||||
!!! error TS2322: Type '(string | Element)[]' is not assignable to type 'string | Element'.
|
||||
!!! error TS2322: Type '(string | Element)[]' is missing the following properties from type 'Element': type, props
|
||||
!!! error TS2322: Type '(string | Element)[]' is not assignable to type 'string'.
|
||||
<div> My Div </div>
|
||||
hi hi hi!
|
||||
</Comp>;
|
||||
@@ -92,7 +92,7 @@ tests/cases/conformance/jsx/file.tsx(49,6): error TS2322: Type '{ children: Elem
|
||||
!!! error TS2322: Type '{ children: Element[]; a: number; b: string; }' is not assignable to type 'Prop'.
|
||||
!!! error TS2322: Types of property 'children' are incompatible.
|
||||
!!! error TS2322: Type 'Element[]' is not assignable to type 'string | Element'.
|
||||
!!! error TS2322: Type 'Element[]' is missing the following properties from type 'Element': type, props
|
||||
!!! error TS2322: Type 'Element[]' is not assignable to type 'string'.
|
||||
<div> My Div </div>
|
||||
<div> My Div </div>
|
||||
</Comp>;
|
||||
@@ -9,12 +9,15 @@ tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(
|
||||
Type 'string | number' is not assignable to type 'number'.
|
||||
Type 'string' is not assignable to type 'number'.
|
||||
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(21,5): error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; }'.
|
||||
Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: number; }'.
|
||||
Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; }'.
|
||||
Types of property 'prop' are incompatible.
|
||||
Type 'string | number' is not assignable to type 'number'.
|
||||
Type 'string' is not assignable to type 'number'.
|
||||
Type 'string | number' is not assignable to type 'string'.
|
||||
Type 'number' is not assignable to type 'string'.
|
||||
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(25,5): error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
|
||||
Property 'anotherP1' is missing in type '{ prop: string | number; anotherP: string; }' but required in type '{ prop: number; anotherP1: number; }'.
|
||||
Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; }'.
|
||||
Types of property 'prop' are incompatible.
|
||||
Type 'string | number' is not assignable to type 'string'.
|
||||
Type 'number' is not assignable to type 'string'.
|
||||
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(29,5): error TS2322: Type '{ prop: string | number; anotherP: string; anotherP1: number; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
|
||||
Type '{ prop: string | number; anotherP: string; anotherP1: number; }' is not assignable to type '{ prop: number; anotherP1: number; }'.
|
||||
Types of property 'prop' are incompatible.
|
||||
@@ -62,18 +65,20 @@ tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(
|
||||
var objStrOrNum6: { prop: string; anotherP: string; } | { prop: number } = {
|
||||
~~~~~~~~~~~~
|
||||
!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; }'.
|
||||
!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: number; }'.
|
||||
!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; }'.
|
||||
!!! error TS2322: Types of property 'prop' are incompatible.
|
||||
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
|
||||
!!! error TS2322: Type 'string' is not assignable to type 'number'.
|
||||
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
|
||||
!!! error TS2322: Type 'number' is not assignable to type 'string'.
|
||||
prop: strOrNumber,
|
||||
anotherP: str
|
||||
};
|
||||
var objStrOrNum7: { prop: string; anotherP: string; } | { prop: number; anotherP1: number } = {
|
||||
~~~~~~~~~~~~
|
||||
!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
|
||||
!!! error TS2322: Property 'anotherP1' is missing in type '{ prop: string | number; anotherP: string; }' but required in type '{ prop: number; anotherP1: number; }'.
|
||||
!!! related TS2728 tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts:25:73: 'anotherP1' is declared here.
|
||||
!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; }'.
|
||||
!!! error TS2322: Types of property 'prop' are incompatible.
|
||||
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
|
||||
!!! error TS2322: Type 'number' is not assignable to type 'string'.
|
||||
prop: strOrNumber,
|
||||
anotherP: str
|
||||
};
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(18,3): error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'.
|
||||
Type '{ a: string; b: string; }' is not assignable to type 'Foo'.
|
||||
Types of property 'b' are incompatible.
|
||||
Type 'string' is not assignable to type 'number'.
|
||||
tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(19,3): error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'.
|
||||
Type '{ a: string; b: string; }' is not assignable to type 'Foo'.
|
||||
Types of property 'b' are incompatible.
|
||||
Type 'string' is not assignable to type 'number'.
|
||||
tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(24,5): error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Bar | Other'.
|
||||
Object literal may only specify known properties, and 'a' does not exist in type 'Bar | Other'.
|
||||
|
||||
|
||||
==== tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts (3 errors) ====
|
||||
interface Foo {
|
||||
a: string;
|
||||
b: number;
|
||||
};
|
||||
|
||||
interface Bar {
|
||||
b: string;
|
||||
}
|
||||
|
||||
interface Other {
|
||||
totallyUnrelatedProperty: number;
|
||||
}
|
||||
|
||||
export let x = { a: '', b: '' };
|
||||
|
||||
declare function f(x: Foo | Other): any;
|
||||
|
||||
f(x);
|
||||
~
|
||||
!!! error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'.
|
||||
!!! error TS2345: Type '{ a: string; b: string; }' is not assignable to type 'Foo'.
|
||||
!!! error TS2345: Types of property 'b' are incompatible.
|
||||
!!! error TS2345: Type 'string' is not assignable to type 'number'.
|
||||
f({ a: '', b: '' })
|
||||
~~~~~~~~~~~~~~~~
|
||||
!!! error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'.
|
||||
!!! error TS2345: Type '{ a: string; b: string; }' is not assignable to type 'Foo'.
|
||||
!!! error TS2345: Types of property 'b' are incompatible.
|
||||
!!! error TS2345: Type 'string' is not assignable to type 'number'.
|
||||
|
||||
declare function g(x: Bar | Other): any;
|
||||
|
||||
g(x);
|
||||
g({ a: '', b: '' })
|
||||
~~~~~
|
||||
!!! error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Bar | Other'.
|
||||
!!! error TS2345: Object literal may only specify known properties, and 'a' does not exist in type 'Bar | Other'.
|
||||
|
||||
declare function h(x: Foo | Bar | Other): any;
|
||||
|
||||
h(x);
|
||||
h({ a: '', b: '' })
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
//// [errorsOnUnionsOfOverlappingObjects01.ts]
|
||||
interface Foo {
|
||||
a: string;
|
||||
b: number;
|
||||
};
|
||||
|
||||
interface Bar {
|
||||
b: string;
|
||||
}
|
||||
|
||||
interface Other {
|
||||
totallyUnrelatedProperty: number;
|
||||
}
|
||||
|
||||
export let x = { a: '', b: '' };
|
||||
|
||||
declare function f(x: Foo | Other): any;
|
||||
|
||||
f(x);
|
||||
f({ a: '', b: '' })
|
||||
|
||||
declare function g(x: Bar | Other): any;
|
||||
|
||||
g(x);
|
||||
g({ a: '', b: '' })
|
||||
|
||||
declare function h(x: Foo | Bar | Other): any;
|
||||
|
||||
h(x);
|
||||
h({ a: '', b: '' })
|
||||
|
||||
|
||||
//// [errorsOnUnionsOfOverlappingObjects01.js]
|
||||
"use strict";
|
||||
exports.__esModule = true;
|
||||
;
|
||||
exports.x = { a: '', b: '' };
|
||||
f(exports.x);
|
||||
f({ a: '', b: '' });
|
||||
g(exports.x);
|
||||
g({ a: '', b: '' });
|
||||
h(exports.x);
|
||||
h({ a: '', b: '' });
|
||||
@@ -0,0 +1,77 @@
|
||||
=== tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts ===
|
||||
interface Foo {
|
||||
>Foo : Symbol(Foo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 0))
|
||||
|
||||
a: string;
|
||||
>a : Symbol(Foo.a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 15))
|
||||
|
||||
b: number;
|
||||
>b : Symbol(Foo.b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 1, 14))
|
||||
|
||||
};
|
||||
|
||||
interface Bar {
|
||||
>Bar : Symbol(Bar, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 3, 2))
|
||||
|
||||
b: string;
|
||||
>b : Symbol(Bar.b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 5, 15))
|
||||
}
|
||||
|
||||
interface Other {
|
||||
>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1))
|
||||
|
||||
totallyUnrelatedProperty: number;
|
||||
>totallyUnrelatedProperty : Symbol(Other.totallyUnrelatedProperty, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 9, 17))
|
||||
}
|
||||
|
||||
export let x = { a: '', b: '' };
|
||||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10))
|
||||
>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 16))
|
||||
>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 23))
|
||||
|
||||
declare function f(x: Foo | Other): any;
|
||||
>f : Symbol(f, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 32))
|
||||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 15, 19))
|
||||
>Foo : Symbol(Foo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 0))
|
||||
>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1))
|
||||
|
||||
f(x);
|
||||
>f : Symbol(f, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 32))
|
||||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10))
|
||||
|
||||
f({ a: '', b: '' })
|
||||
>f : Symbol(f, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 32))
|
||||
>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 3))
|
||||
>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 10))
|
||||
|
||||
declare function g(x: Bar | Other): any;
|
||||
>g : Symbol(g, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 19))
|
||||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 20, 19))
|
||||
>Bar : Symbol(Bar, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 3, 2))
|
||||
>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1))
|
||||
|
||||
g(x);
|
||||
>g : Symbol(g, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 19))
|
||||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10))
|
||||
|
||||
g({ a: '', b: '' })
|
||||
>g : Symbol(g, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 19))
|
||||
>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 3))
|
||||
>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 10))
|
||||
|
||||
declare function h(x: Foo | Bar | Other): any;
|
||||
>h : Symbol(h, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 19))
|
||||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 25, 19))
|
||||
>Foo : Symbol(Foo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 0))
|
||||
>Bar : Symbol(Bar, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 3, 2))
|
||||
>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1))
|
||||
|
||||
h(x);
|
||||
>h : Symbol(h, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 19))
|
||||
>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10))
|
||||
|
||||
h({ a: '', b: '' })
|
||||
>h : Symbol(h, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 19))
|
||||
>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 28, 3))
|
||||
>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 28, 10))
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
=== tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts ===
|
||||
interface Foo {
|
||||
a: string;
|
||||
>a : string
|
||||
|
||||
b: number;
|
||||
>b : number
|
||||
|
||||
};
|
||||
|
||||
interface Bar {
|
||||
b: string;
|
||||
>b : string
|
||||
}
|
||||
|
||||
interface Other {
|
||||
totallyUnrelatedProperty: number;
|
||||
>totallyUnrelatedProperty : number
|
||||
}
|
||||
|
||||
export let x = { a: '', b: '' };
|
||||
>x : { a: string; b: string; }
|
||||
>{ a: '', b: '' } : { a: string; b: string; }
|
||||
>a : string
|
||||
>'' : ""
|
||||
>b : string
|
||||
>'' : ""
|
||||
|
||||
declare function f(x: Foo | Other): any;
|
||||
>f : (x: Foo | Other) => any
|
||||
>x : Foo | Other
|
||||
|
||||
f(x);
|
||||
>f(x) : any
|
||||
>f : (x: Foo | Other) => any
|
||||
>x : { a: string; b: string; }
|
||||
|
||||
f({ a: '', b: '' })
|
||||
>f({ a: '', b: '' }) : any
|
||||
>f : (x: Foo | Other) => any
|
||||
>{ a: '', b: '' } : { a: string; b: string; }
|
||||
>a : string
|
||||
>'' : ""
|
||||
>b : string
|
||||
>'' : ""
|
||||
|
||||
declare function g(x: Bar | Other): any;
|
||||
>g : (x: Bar | Other) => any
|
||||
>x : Bar | Other
|
||||
|
||||
g(x);
|
||||
>g(x) : any
|
||||
>g : (x: Bar | Other) => any
|
||||
>x : { a: string; b: string; }
|
||||
|
||||
g({ a: '', b: '' })
|
||||
>g({ a: '', b: '' }) : any
|
||||
>g : (x: Bar | Other) => any
|
||||
>{ a: '', b: '' } : { a: string; b: string; }
|
||||
>a : string
|
||||
>'' : ""
|
||||
>b : string
|
||||
>'' : ""
|
||||
|
||||
declare function h(x: Foo | Bar | Other): any;
|
||||
>h : (x: Foo | Bar | Other) => any
|
||||
>x : Foo | Bar | Other
|
||||
|
||||
h(x);
|
||||
>h(x) : any
|
||||
>h : (x: Foo | Bar | Other) => any
|
||||
>x : { a: string; b: string; }
|
||||
|
||||
h({ a: '', b: '' })
|
||||
>h({ a: '', b: '' }) : any
|
||||
>h : (x: Foo | Bar | Other) => any
|
||||
>{ a: '', b: '' } : { a: string; b: string; }
|
||||
>a : string
|
||||
>'' : ""
|
||||
>b : string
|
||||
>'' : ""
|
||||
|
||||
@@ -13,9 +13,9 @@ tests/cases/compiler/excessPropertyCheckWithUnions.ts(39,1): error TS2322: Type
|
||||
Types of property 'tag' are incompatible.
|
||||
Type '"A"' is not assignable to type '"C"'.
|
||||
tests/cases/compiler/excessPropertyCheckWithUnions.ts(40,1): error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type 'Ambiguous'.
|
||||
Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "C"; }'.
|
||||
Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "B"; z: boolean; }'.
|
||||
Types of property 'tag' are incompatible.
|
||||
Type '"A"' is not assignable to type '"C"'.
|
||||
Type '"A"' is not assignable to type '"B"'.
|
||||
tests/cases/compiler/excessPropertyCheckWithUnions.ts(49,35): error TS2322: Type '{ a: 1; b: 1; first: string; second: string; }' is not assignable to type 'Overlapping'.
|
||||
Object literal may only specify known properties, and 'second' does not exist in type '{ a: 1; b: 1; first: string; }'.
|
||||
tests/cases/compiler/excessPropertyCheckWithUnions.ts(50,35): error TS2322: Type '{ a: 1; b: 1; first: string; third: string; }' is not assignable to type 'Overlapping'.
|
||||
@@ -91,9 +91,9 @@ tests/cases/compiler/excessPropertyCheckWithUnions.ts(66,9): error TS2322: Type
|
||||
amb = { tag: "A", z: true }
|
||||
~~~
|
||||
!!! error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type 'Ambiguous'.
|
||||
!!! error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "C"; }'.
|
||||
!!! error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "B"; z: boolean; }'.
|
||||
!!! error TS2322: Types of property 'tag' are incompatible.
|
||||
!!! error TS2322: Type '"A"' is not assignable to type '"C"'.
|
||||
!!! error TS2322: Type '"A"' is not assignable to type '"B"'.
|
||||
|
||||
type Overlapping =
|
||||
| { a: 1, b: 1, first: string }
|
||||
|
||||
@@ -18,7 +18,7 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(75,43): error TS4081: E
|
||||
tests/cases/conformance/types/conditional/inferTypes1.ts(82,44): error TS2344: Type 'U' does not satisfy the constraint 'string'.
|
||||
Type 'number' is not assignable to type 'string'.
|
||||
tests/cases/conformance/types/conditional/inferTypes1.ts(144,40): error TS2322: Type 'T' is not assignable to type 'string | number | symbol'.
|
||||
Type 'T' is not assignable to type 'symbol'.
|
||||
Type 'T' is not assignable to type 'string'.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/conditional/inferTypes1.ts (16 errors) ====
|
||||
@@ -202,7 +202,7 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(144,40): error TS2322:
|
||||
type B<T> = string extends T ? { [P in T]: void; } : T; // Error
|
||||
~
|
||||
!!! error TS2322: Type 'T' is not assignable to type 'string | number | symbol'.
|
||||
!!! error TS2322: Type 'T' is not assignable to type 'symbol'.
|
||||
!!! error TS2322: Type 'T' is not assignable to type 'string'.
|
||||
|
||||
// Repro from #22302
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(20,1): e
|
||||
Property 'a' is missing in type 'B' but required in type 'A'.
|
||||
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(23,1): error TS2322: Type 'A | B' is not assignable to type '(A & B) | (C & D)'.
|
||||
Type 'A' is not assignable to type '(A & B) | (C & D)'.
|
||||
Type 'A' is not assignable to type 'C & D'.
|
||||
Property 'c' is missing in type 'A' but required in type 'C'.
|
||||
Type 'A' is not assignable to type 'A & B'.
|
||||
Type 'A' is not assignable to type 'B'.
|
||||
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(25,1): error TS2322: Type 'C | D' is not assignable to type '(A & B) | (C & D)'.
|
||||
Type 'C' is not assignable to type '(A & B) | (C & D)'.
|
||||
Type 'C' is not assignable to type 'C & D'.
|
||||
@@ -27,14 +27,14 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(31,1): e
|
||||
Type 'A & B' is not assignable to type 'D'.
|
||||
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(32,1): error TS2322: Type 'A | B' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
|
||||
Type 'A' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
|
||||
Type 'A' is not assignable to type 'B & D'.
|
||||
Type 'A' is not assignable to type 'B'.
|
||||
Type 'A' is not assignable to type 'A & D'.
|
||||
Property 'd' is missing in type 'A' but required in type 'D'.
|
||||
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(33,1): error TS2322: Type 'C & D' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
|
||||
Type 'C & D' is not assignable to type 'B & D'.
|
||||
Type 'C & D' is not assignable to type 'B'.
|
||||
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(34,1): error TS2322: Type 'C | D' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
|
||||
Type 'C' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
|
||||
Type 'C' is not assignable to type 'B & D'.
|
||||
Type 'C' is not assignable to type 'B & C'.
|
||||
Property 'b' is missing in type 'C' but required in type 'B'.
|
||||
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(35,1): error TS2322: Type '(A & C) | (A & D) | (B & C) | (B & D)' is not assignable to type 'A & B'.
|
||||
Type 'A & C' is not assignable to type 'A & B'.
|
||||
@@ -79,9 +79,8 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(37,1): e
|
||||
~
|
||||
!!! error TS2322: Type 'A | B' is not assignable to type '(A & B) | (C & D)'.
|
||||
!!! error TS2322: Type 'A' is not assignable to type '(A & B) | (C & D)'.
|
||||
!!! error TS2322: Type 'A' is not assignable to type 'C & D'.
|
||||
!!! error TS2322: Property 'c' is missing in type 'A' but required in type 'C'.
|
||||
!!! related TS2728 tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts:3:15: 'c' is declared here.
|
||||
!!! error TS2322: Type 'A' is not assignable to type 'A & B'.
|
||||
!!! error TS2322: Type 'A' is not assignable to type 'B'.
|
||||
x = cnd; // Ok
|
||||
x = cod;
|
||||
~
|
||||
@@ -124,8 +123,9 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(37,1): e
|
||||
~
|
||||
!!! error TS2322: Type 'A | B' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
|
||||
!!! error TS2322: Type 'A' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
|
||||
!!! error TS2322: Type 'A' is not assignable to type 'B & D'.
|
||||
!!! error TS2322: Type 'A' is not assignable to type 'B'.
|
||||
!!! error TS2322: Type 'A' is not assignable to type 'A & D'.
|
||||
!!! error TS2322: Property 'd' is missing in type 'A' but required in type 'D'.
|
||||
!!! related TS2728 tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts:4:15: 'd' is declared here.
|
||||
y = cnd;
|
||||
~
|
||||
!!! error TS2322: Type 'C & D' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
|
||||
@@ -135,7 +135,7 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(37,1): e
|
||||
~
|
||||
!!! error TS2322: Type 'C | D' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
|
||||
!!! error TS2322: Type 'C' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
|
||||
!!! error TS2322: Type 'C' is not assignable to type 'B & D'.
|
||||
!!! error TS2322: Type 'C' is not assignable to type 'B & C'.
|
||||
!!! error TS2322: Property 'b' is missing in type 'C' but required in type 'B'.
|
||||
!!! related TS2728 tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts:2:15: 'b' is declared here.
|
||||
anb = y;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(7,9): error TS2322: Type 'T & U' is not assignable to type 'string | number'.
|
||||
Type 'string | undefined' is not assignable to type 'string | number'.
|
||||
Type 'undefined' is not assignable to type 'string | number'.
|
||||
Type 'T & U' is not assignable to type 'number'.
|
||||
Type 'T & U' is not assignable to type 'string'.
|
||||
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(8,9): error TS2322: Type 'T & U' is not assignable to type 'string | null'.
|
||||
Type 'string | undefined' is not assignable to type 'string | null'.
|
||||
Type 'undefined' is not assignable to type 'string | null'.
|
||||
@@ -32,7 +32,7 @@ tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12
|
||||
!!! error TS2322: Type 'T & U' is not assignable to type 'string | number'.
|
||||
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string | number'.
|
||||
!!! error TS2322: Type 'undefined' is not assignable to type 'string | number'.
|
||||
!!! error TS2322: Type 'T & U' is not assignable to type 'number'.
|
||||
!!! error TS2322: Type 'T & U' is not assignable to type 'string'.
|
||||
let y2: string | null = x; // Error
|
||||
~~
|
||||
!!! error TS2322: Type 'T & U' is not assignable to type 'string | null'.
|
||||
|
||||
@@ -58,7 +58,7 @@ type K00 = keyof any; // string
|
||||
>K00 : string | number | symbol
|
||||
|
||||
type K01 = keyof string; // "toString" | "charAt" | ...
|
||||
>K01 : number | "length" | "toString" | "concat" | "slice" | "indexOf" | "lastIndexOf" | "charAt" | "charCodeAt" | "localeCompare" | "match" | "replace" | "search" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr" | "valueOf"
|
||||
>K01 : number | "length" | "toString" | "concat" | "slice" | "indexOf" | "lastIndexOf" | "valueOf" | "charAt" | "charCodeAt" | "localeCompare" | "match" | "replace" | "search" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
|
||||
|
||||
type K02 = keyof number; // "toString" | "toFixed" | "toExponential" | ...
|
||||
>K02 : "toString" | "toLocaleString" | "valueOf" | "toFixed" | "toExponential" | "toPrecision"
|
||||
|
||||
@@ -23,7 +23,7 @@ type T00 = keyof K0; // Error
|
||||
>T00 : string | number | symbol
|
||||
|
||||
type T01 = keyof Object;
|
||||
>T01 : "toString" | "constructor" | "toLocaleString" | "valueOf" | "hasOwnProperty" | "isPrototypeOf" | "propertyIsEnumerable"
|
||||
>T01 : "toString" | "valueOf" | "toLocaleString" | "constructor" | "hasOwnProperty" | "isPrototypeOf" | "propertyIsEnumerable"
|
||||
|
||||
type T02 = keyof keyof Object;
|
||||
>T02 : number | "length" | "toString" | "valueOf" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(19,20): error TS2313: Type parameter 'P' has a circular constraint.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(21,20): error TS2322: Type 'Date' is not assignable to type 'string | number | symbol'.
|
||||
Type 'Date' is not assignable to type 'symbol'.
|
||||
Type 'Date' is not assignable to type 'number'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(22,19): error TS2344: Type 'Date' does not satisfy the constraint 'string | number | symbol'.
|
||||
Type 'Date' is not assignable to type 'symbol'.
|
||||
Type 'Date' is not assignable to type 'number'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(25,24): error TS2344: Type '"foo"' does not satisfy the constraint '"name" | "width" | "height" | "visible"'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(26,24): error TS2344: Type '"name" | "foo"' does not satisfy the constraint '"name" | "width" | "height" | "visible"'.
|
||||
Type '"foo"' is not assignable to type '"name" | "width" | "height" | "visible"'.
|
||||
@@ -37,7 +37,7 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(128,16): error TS2322:
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(129,25): error TS2322: Type 'string' is not assignable to type 'number | undefined'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(130,39): error TS2322: Type 'string' is not assignable to type 'number | undefined'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(136,16): error TS2322: Type 'T' is not assignable to type 'string | number | symbol'.
|
||||
Type 'T' is not assignable to type 'symbol'.
|
||||
Type 'T' is not assignable to type 'string'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(136,21): error TS2536: Type 'P' cannot be used to index type 'T'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(148,17): error TS2339: Property 'foo' does not exist on type 'Pick<T, K>'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(152,17): error TS2339: Property 'foo' does not exist on type 'Record<K, number>'.
|
||||
@@ -69,11 +69,11 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(152,17): error TS2339:
|
||||
type T02 = { [P in Date]: number }; // Error
|
||||
~~~~
|
||||
!!! error TS2322: Type 'Date' is not assignable to type 'string | number | symbol'.
|
||||
!!! error TS2322: Type 'Date' is not assignable to type 'symbol'.
|
||||
!!! error TS2322: Type 'Date' is not assignable to type 'number'.
|
||||
type T03 = Record<Date, number>; // Error
|
||||
~~~~
|
||||
!!! error TS2344: Type 'Date' does not satisfy the constraint 'string | number | symbol'.
|
||||
!!! error TS2344: Type 'Date' is not assignable to type 'symbol'.
|
||||
!!! error TS2344: Type 'Date' is not assignable to type 'number'.
|
||||
|
||||
type T10 = Pick<Shape, "name">;
|
||||
type T11 = Pick<Shape, "foo">; // Error
|
||||
@@ -249,7 +249,7 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(152,17): error TS2339:
|
||||
pt: {[P in T]?: T[P]}, // note: should be in keyof T
|
||||
~
|
||||
!!! error TS2322: Type 'T' is not assignable to type 'string | number | symbol'.
|
||||
!!! error TS2322: Type 'T' is not assignable to type 'symbol'.
|
||||
!!! error TS2322: Type 'T' is not assignable to type 'string'.
|
||||
~~~~
|
||||
!!! error TS2536: Type 'P' cannot be used to index type 'T'.
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@ tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(9,30): error TS2536: T
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(13,30): error TS2536: Type 'K' cannot be used to index type 'T3'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,38): error TS2536: Type 'S' cannot be used to index type '{ [key in AB[S]]: true; }'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,47): error TS2322: Type 'AB[S]' is not assignable to type 'string | number | symbol'.
|
||||
Type 'AB[S]' is not assignable to type 'symbol'.
|
||||
Type 'AB[S]' is not assignable to type 'string'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,47): error TS2536: Type 'S' cannot be used to index type 'AB'.
|
||||
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(17,49): error TS2536: Type 'L' cannot be used to index type '{ [key in AB[S]]: true; }'.
|
||||
|
||||
@@ -31,7 +31,7 @@ tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(17,49): error TS2536:
|
||||
!!! error TS2536: Type 'S' cannot be used to index type '{ [key in AB[S]]: true; }'.
|
||||
~~~~~
|
||||
!!! error TS2322: Type 'AB[S]' is not assignable to type 'string | number | symbol'.
|
||||
!!! error TS2322: Type 'AB[S]' is not assignable to type 'symbol'.
|
||||
!!! error TS2322: Type 'AB[S]' is not assignable to type 'string'.
|
||||
~~~~~
|
||||
!!! error TS2536: Type 'S' cannot be used to index type 'AB'.
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
interface Foo {
|
||||
a: string;
|
||||
b: number;
|
||||
};
|
||||
|
||||
interface Bar {
|
||||
b: string;
|
||||
}
|
||||
|
||||
interface Other {
|
||||
totallyUnrelatedProperty: number;
|
||||
}
|
||||
|
||||
export let x = { a: '', b: '' };
|
||||
|
||||
declare function f(x: Foo | Other): any;
|
||||
|
||||
f(x);
|
||||
f({ a: '', b: '' })
|
||||
|
||||
declare function g(x: Bar | Other): any;
|
||||
|
||||
g(x);
|
||||
g({ a: '', b: '' })
|
||||
|
||||
declare function h(x: Foo | Bar | Other): any;
|
||||
|
||||
h(x);
|
||||
h({ a: '', b: '' })
|
||||
Reference in New Issue
Block a user