-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Compare optional property flag when comparing against discriminant properties under exactOptionalPropertyTypes
#61682
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But that's not true under |
||
// If the target property could not be found, or if the properties were not related, | ||
// then this constituent is not a match. | ||
if (!related) { | ||
|
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)) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding parentheses to clarify the evaluation order of the skipOptional argument. For example, change it to '(strictNullChecks && !exactOptionalPropertyTypes) || relation === comparableRelation' to ensure the intended precedence and improve code readability.
Copilot uses AI. Check for mistakes.