Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24166,7 +24166,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (!targetProperty) continue outer;
if (sourceProperty === targetProperty) continue;
// We compare the source property to the target in the context of a single discriminant type.
const related = propertyRelatedTo(source, target, sourceProperty, targetProperty, _ => combination[i], /*reportErrors*/ false, IntersectionState.None, /*skipOptional*/ strictNullChecks || relation === comparableRelation);
const related = propertyRelatedTo(source, target, sourceProperty, targetProperty, _ => combination[i], /*reportErrors*/ false, IntersectionState.None, /*skipOptional*/ strictNullChecks && !exactOptionalPropertyTypes || relation === comparableRelation);
// If the target property could not be found, or if the properties were not related,
// then this constituent is not a match.
if (!related) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
unionRelationshipCheckPasses2.ts(10,3): error TS2322: Type '{ type: "A"; value?: undefined; } | { type: "B"; value: string; }' is not assignable to type 'U'.
Type '{ type: "A"; value?: undefined; }' is not assignable to type 'U'.
Type '{ type: "A"; value?: undefined; }' is not assignable to type '{ type: "A"; value: null; }'.
Types of property 'value' are incompatible.
Type 'undefined' is not assignable to type 'null'.
unionRelationshipCheckPasses2.ts(21,5): error TS2322: Type '{ type: "A"; }' is not assignable to type 'U'.
Property 'value' is missing in type '{ type: "A"; }' but required in type '{ type: "A"; value: null; }'.


==== unionRelationshipCheckPasses2.ts (2 errors) ====
// https://github.com/microsoft/TypeScript/issues/61678

export type U = { type: "A"; value: null } | { type: "B"; value: string };

function call<T>(f: () => T): T {
return f();
}

export function functionCall(): U {
return call(() => { // error
~~~~~~
!!! error TS2322: Type '{ type: "A"; value?: undefined; } | { type: "B"; value: string; }' is not assignable to type 'U'.
!!! error TS2322: Type '{ type: "A"; value?: undefined; }' is not assignable to type 'U'.
!!! error TS2322: Type '{ type: "A"; value?: undefined; }' is not assignable to type '{ type: "A"; value: null; }'.
!!! error TS2322: Types of property 'value' are incompatible.
!!! error TS2322: Type 'undefined' is not assignable to type 'null'.
if (Math.random()) {
return { type: "A" };
}

return { type: "B", value: "test" };
});
}

export function directReturn(): U {
if (Math.random()) {
return { type: "A" }; // error
~~~~~~
!!! error TS2322: Type '{ type: "A"; }' is not assignable to type 'U'.
!!! error TS2322: Property 'value' is missing in type '{ type: "A"; }' but required in type '{ type: "A"; value: null; }'.
!!! related TS2728 unionRelationshipCheckPasses2.ts:3:30: 'value' is declared here.
}

return { type: "B", value: "test" };
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//// [tests/cases/compiler/unionRelationshipCheckPasses2.ts] ////

=== unionRelationshipCheckPasses2.ts ===
// https://github.com/microsoft/TypeScript/issues/61678

export type U = { type: "A"; value: null } | { type: "B"; value: string };
>U : Symbol(U, Decl(unionRelationshipCheckPasses2.ts, 0, 0))
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 2, 17))
>value : Symbol(value, Decl(unionRelationshipCheckPasses2.ts, 2, 28))
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 2, 46))
>value : Symbol(value, Decl(unionRelationshipCheckPasses2.ts, 2, 57))

function call<T>(f: () => T): T {
>call : Symbol(call, Decl(unionRelationshipCheckPasses2.ts, 2, 74))
>T : Symbol(T, Decl(unionRelationshipCheckPasses2.ts, 4, 14))
>f : Symbol(f, Decl(unionRelationshipCheckPasses2.ts, 4, 17))
>T : Symbol(T, Decl(unionRelationshipCheckPasses2.ts, 4, 14))
>T : Symbol(T, Decl(unionRelationshipCheckPasses2.ts, 4, 14))

return f();
>f : Symbol(f, Decl(unionRelationshipCheckPasses2.ts, 4, 17))
}

export function functionCall(): U {
>functionCall : Symbol(functionCall, Decl(unionRelationshipCheckPasses2.ts, 6, 1))
>U : Symbol(U, Decl(unionRelationshipCheckPasses2.ts, 0, 0))

return call(() => { // error
>call : Symbol(call, Decl(unionRelationshipCheckPasses2.ts, 2, 74))

if (Math.random()) {
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

return { type: "A" };
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 11, 14))
}

return { type: "B", value: "test" };
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 14, 12))
>value : Symbol(value, Decl(unionRelationshipCheckPasses2.ts, 14, 23))

});
}

export function directReturn(): U {
>directReturn : Symbol(directReturn, Decl(unionRelationshipCheckPasses2.ts, 16, 1))
>U : Symbol(U, Decl(unionRelationshipCheckPasses2.ts, 0, 0))

if (Math.random()) {
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

return { type: "A" }; // error
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 20, 12))
}

return { type: "B", value: "test" };
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 23, 10))
>value : Symbol(value, Decl(unionRelationshipCheckPasses2.ts, 23, 21))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//// [tests/cases/compiler/unionRelationshipCheckPasses2.ts] ////

=== unionRelationshipCheckPasses2.ts ===
// https://github.com/microsoft/TypeScript/issues/61678

export type U = { type: "A"; value: null } | { type: "B"; value: string };
>U : U
> : ^
>type : "A"
> : ^^^
>value : null
> : ^^^^
>type : "B"
> : ^^^
>value : string
> : ^^^^^^

function call<T>(f: () => T): T {
>call : <T>(f: () => T) => T
> : ^ ^^ ^^ ^^^^^
>f : () => T
> : ^^^^^^

return f();
>f() : T
> : ^
>f : () => T
> : ^^^^^^
}

export function functionCall(): U {
>functionCall : () => U
> : ^^^^^^

return call(() => { // error
>call(() => { // error if (Math.random()) { return { type: "A" }; } return { type: "B", value: "test" }; }) : { type: "A"; value?: undefined; } | { type: "B"; value: string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>call : <T>(f: () => T) => T
> : ^ ^^ ^^ ^^^^^
>() => { // error if (Math.random()) { return { type: "A" }; } return { type: "B", value: "test" }; } : () => { type: "A"; value?: undefined; } | { type: "B"; value: string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

if (Math.random()) {
>Math.random() : number
> : ^^^^^^
>Math.random : () => number
> : ^^^^^^
>Math : Math
> : ^^^^
>random : () => number
> : ^^^^^^

return { type: "A" };
>{ type: "A" } : { type: "A"; }
> : ^^^^^^^^^^^^^^
>type : "A"
> : ^^^
>"A" : "A"
> : ^^^
}

return { type: "B", value: "test" };
>{ type: "B", value: "test" } : { type: "B"; value: string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>type : "B"
> : ^^^
>"B" : "B"
> : ^^^
>value : string
> : ^^^^^^
>"test" : "test"
> : ^^^^^^

});
}

export function directReturn(): U {
>directReturn : () => U
> : ^^^^^^

if (Math.random()) {
>Math.random() : number
> : ^^^^^^
>Math.random : () => number
> : ^^^^^^
>Math : Math
> : ^^^^
>random : () => number
> : ^^^^^^

return { type: "A" }; // error
>{ type: "A" } : { type: "A"; }
> : ^^^^^^^^^^^^^^
>type : "A"
> : ^^^
>"A" : "A"
> : ^^^
}

return { type: "B", value: "test" };
>{ type: "B", value: "test" } : { type: "B"; value: string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>type : "B"
> : ^^^
>"B" : "B"
> : ^^^
>value : string
> : ^^^^^^
>"test" : "test"
> : ^^^^^^
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
unionRelationshipCheckPasses2.ts(10,3): error TS2322: Type '{ type: "A"; value?: never; } | { type: "B"; value: string; }' is not assignable to type 'U'.
Type '{ type: "A"; value?: never; }' is not assignable to type 'U'.
Type '{ type: "A"; value?: never; }' is not assignable to type '{ type: "A"; value: null; }'.
Property 'value' is optional in type '{ type: "A"; value?: never; }' but required in type '{ type: "A"; value: null; }'.
unionRelationshipCheckPasses2.ts(21,5): error TS2322: Type '{ type: "A"; }' is not assignable to type 'U'.
Property 'value' is missing in type '{ type: "A"; }' but required in type '{ type: "A"; value: null; }'.


==== unionRelationshipCheckPasses2.ts (2 errors) ====
// https://github.com/microsoft/TypeScript/issues/61678

export type U = { type: "A"; value: null } | { type: "B"; value: string };

function call<T>(f: () => T): T {
return f();
}

export function functionCall(): U {
return call(() => { // error
~~~~~~
!!! error TS2322: Type '{ type: "A"; value?: never; } | { type: "B"; value: string; }' is not assignable to type 'U'.
!!! error TS2322: Type '{ type: "A"; value?: never; }' is not assignable to type 'U'.
!!! error TS2322: Type '{ type: "A"; value?: never; }' is not assignable to type '{ type: "A"; value: null; }'.
!!! error TS2322: Property 'value' is optional in type '{ type: "A"; value?: never; }' but required in type '{ type: "A"; value: null; }'.
if (Math.random()) {
return { type: "A" };
}

return { type: "B", value: "test" };
});
}

export function directReturn(): U {
if (Math.random()) {
return { type: "A" }; // error
~~~~~~
!!! error TS2322: Type '{ type: "A"; }' is not assignable to type 'U'.
!!! error TS2322: Property 'value' is missing in type '{ type: "A"; }' but required in type '{ type: "A"; value: null; }'.
!!! related TS2728 unionRelationshipCheckPasses2.ts:3:30: 'value' is declared here.
}

return { type: "B", value: "test" };
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//// [tests/cases/compiler/unionRelationshipCheckPasses2.ts] ////

=== unionRelationshipCheckPasses2.ts ===
// https://github.com/microsoft/TypeScript/issues/61678

export type U = { type: "A"; value: null } | { type: "B"; value: string };
>U : Symbol(U, Decl(unionRelationshipCheckPasses2.ts, 0, 0))
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 2, 17))
>value : Symbol(value, Decl(unionRelationshipCheckPasses2.ts, 2, 28))
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 2, 46))
>value : Symbol(value, Decl(unionRelationshipCheckPasses2.ts, 2, 57))

function call<T>(f: () => T): T {
>call : Symbol(call, Decl(unionRelationshipCheckPasses2.ts, 2, 74))
>T : Symbol(T, Decl(unionRelationshipCheckPasses2.ts, 4, 14))
>f : Symbol(f, Decl(unionRelationshipCheckPasses2.ts, 4, 17))
>T : Symbol(T, Decl(unionRelationshipCheckPasses2.ts, 4, 14))
>T : Symbol(T, Decl(unionRelationshipCheckPasses2.ts, 4, 14))

return f();
>f : Symbol(f, Decl(unionRelationshipCheckPasses2.ts, 4, 17))
}

export function functionCall(): U {
>functionCall : Symbol(functionCall, Decl(unionRelationshipCheckPasses2.ts, 6, 1))
>U : Symbol(U, Decl(unionRelationshipCheckPasses2.ts, 0, 0))

return call(() => { // error
>call : Symbol(call, Decl(unionRelationshipCheckPasses2.ts, 2, 74))

if (Math.random()) {
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

return { type: "A" };
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 11, 14))
}

return { type: "B", value: "test" };
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 14, 12))
>value : Symbol(value, Decl(unionRelationshipCheckPasses2.ts, 14, 23))

});
}

export function directReturn(): U {
>directReturn : Symbol(directReturn, Decl(unionRelationshipCheckPasses2.ts, 16, 1))
>U : Symbol(U, Decl(unionRelationshipCheckPasses2.ts, 0, 0))

if (Math.random()) {
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

return { type: "A" }; // error
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 20, 12))
}

return { type: "B", value: "test" };
>type : Symbol(type, Decl(unionRelationshipCheckPasses2.ts, 23, 10))
>value : Symbol(value, Decl(unionRelationshipCheckPasses2.ts, 23, 21))
}

Loading