-
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?
Conversation
…operties under `exactOptionalPropertyTypes`
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.
Pull Request Overview
This PR fixes an issue (#61678) in which optional property flags are compared incorrectly against discriminant properties when the compiler flag exactOptionalPropertyTypes is enabled. Key changes include updated test cases that expect errors in union discriminant assignments and a modification of the comparison logic in the checker.
Reviewed Changes
Copilot reviewed 3 out of 15 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
tests/cases/compiler/unionRelationshipCheckPasses3.ts | New test case to verify union assignment errors with different optional property flags |
tests/cases/compiler/unionRelationshipCheckPasses2.ts | Additional test confirming error handling with union discriminants |
src/compiler/checker.ts | Updated property comparison logic to consider exactOptionalPropertyTypes when checking optional properties |
Files not reviewed (12)
- tests/baselines/reference/unionRelationshipCheckPasses2(exactoptionalpropertytypes=false).errors.txt: Language not supported
- tests/baselines/reference/unionRelationshipCheckPasses2(exactoptionalpropertytypes=false).symbols: Language not supported
- tests/baselines/reference/unionRelationshipCheckPasses2(exactoptionalpropertytypes=false).types: Language not supported
- tests/baselines/reference/unionRelationshipCheckPasses2(exactoptionalpropertytypes=true).errors.txt: Language not supported
- tests/baselines/reference/unionRelationshipCheckPasses2(exactoptionalpropertytypes=true).symbols: Language not supported
- tests/baselines/reference/unionRelationshipCheckPasses2(exactoptionalpropertytypes=true).types: Language not supported
- tests/baselines/reference/unionRelationshipCheckPasses3(exactoptionalpropertytypes=false).errors.txt: Language not supported
- tests/baselines/reference/unionRelationshipCheckPasses3(exactoptionalpropertytypes=false).symbols: Language not supported
- tests/baselines/reference/unionRelationshipCheckPasses3(exactoptionalpropertytypes=false).types: Language not supported
- tests/baselines/reference/unionRelationshipCheckPasses3(exactoptionalpropertytypes=true).errors.txt: Language not supported
- tests/baselines/reference/unionRelationshipCheckPasses3(exactoptionalpropertytypes=true).symbols: Language not supported
- tests/baselines/reference/unionRelationshipCheckPasses3(exactoptionalpropertytypes=true).types: Language not supported
@@ -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 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.
@@ -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 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.
@typescript-bot test it |
Hey @RyanCavanaugh, the results of running the DT tests are ready. Everything looks the same! |
@RyanCavanaugh Here are the results of running the user tests with tsc comparing Everything looks good! |
@RyanCavanaugh Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@RyanCavanaugh Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
fixes #61678