Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -23998,7 +23998,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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skipOptional got introduced in #38101 . According to that PR's description its main goal is to align with this principle:

an optional undefined is the same as an undefined field in everything except what we require be written in declarations

But that's not true under exactOptionalPropertyTypes. So the easiest fix to this issue seems to be to just pass skipOptional === false when exactOptionalPropertyTypes is enabled.

// 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, --, --))
>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, --, --))
>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, --, --))
>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, --, --))
>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