-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Error on destructured nullables #60121
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?
Error on destructured nullables #60121
Conversation
|
@typescript-bot test it |
| const { p1 } = f(); // error | ||
| ~~ | ||
| !!! error TS2339: Property 'p1' does not exist on type 'unknown'. | ||
| ~~~ | ||
| !!! error TS2571: Object is of type 'unknown'. |
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.
This gets a little bit noisy because we essentially report the same problem twice.
There are cases in the changed baselines that require the error to be reported on the initializer, like here:
TypeScript/tests/baselines/reference/destructuringArrayBindingPatternAndAssignment1ES5.errors.txt
Lines 32 to 37 in 28cdebc
| var [a0, a1]: any = undefined; | |
| ~~~~~~~~~ | |
| !!! error TS18050: The value 'undefined' cannot be used here. | |
| var [a2 = false, a3 = 1]: any = undefined; | |
| ~~~~~~~~~ | |
| !!! error TS18050: The value 'undefined' cannot be used here. |
So if there is desire to limit the amount of reported errors I think it would be good to report consistently them on the initializer at all times. I think handling this differently for different kind of patterns will complicate the code. But if there is a strong belief that this would provide a better DX - I can implement the required changes.
|
@jakebailey Here are the results of running the user tests with tsc comparing Something interesting changed - please have a look. Details
|
|
Hey @jakebailey, the results of running the DT tests are ready. Everything looks the same! |
|
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@jakebailey Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
benjaminjkraft
left a comment
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.
Neat, thanks! I'm not competent to review the code but I took a glance at the tests.
Does this work right if it's deeper inside the restructuring? The actual code that led to this is more like
const { f: { [k]: v } } = /* value that could be `{ f: undefined }` */(Maybe if you know the code better it obviously does handle this, but my quick glance made me think it might not.)
|
@benjaminjkraft good catch, this wasn't handled - I just pushed out a fix for this. @jakebailey could you rerun tests? the webpack break seems to be OK but the Effect's one is fishy - if you could build a playground for me that would help me with bisecting their repo to a repro case |
|
@typescript-bot test it |
|
Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your and then running There is also a playground for this build and an npm module you can use via |
|
@jakebailey Here are the results of running the user tests with tsc comparing Something interesting changed - please have a look. Details
|
|
Hey @jakebailey, the results of running the DT tests are ready. There were interesting changes: Branch only errors:Package: openfin |
|
DT break comes from a declaration like this: export declare function focus({
emitSynthFocused,
}?: {
emitSynthFocused: boolean;
}): Promise<void>;that can be legally emitted from (TS playground): export async function focus(
{
emitSynthFocused,
}: {
emitSynthFocused: boolean;
} = { emitSynthFocused: true },
) {}I'll have to ignore this check for declaration nodes |
|
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Effect's break is spooky. I can't repro it in the IDE, I can't repro it by running |
|
@jakebailey Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
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 addresses destructuring errors with nullable initializers by implementing proper null checking for destructured parameters and values. The fix ensures that destructuring patterns with potentially null or undefined values generate appropriate error messages.
Key Changes:
- Adds null checking for destructuring patterns when no default initializer is provided
- Replaces expression checking with explicit non-null expression checking for destructuring initializers
- Updates error handling to provide specific nullable error messages (TS18047, TS18048, TS18049, TS18050)
Reviewed Changes
Copilot reviewed 39 out of 39 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/compiler/checker.ts | Core implementation that adds null checking for binding element parents and non-null expression checking for destructuring initializers |
| tests/cases/conformance/es6/destructuring/bindingPatternWithNullableInitializer*.ts | New test cases covering destructuring with nullable types including undefined, null, and union types |
| tests/baselines/reference/*.errors.txt | Updated baseline files showing new error messages for destructuring nullable values |
|
@jakebailey, could you run the tests and build the playground for this again? |
|
@typescript-bot test it |
|
Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your and then running There is also a playground for this build and an npm module you can use via |
|
Hey @jakebailey, the results of running the DT tests are ready. There were interesting changes: Branch only errors:Package: openfin |
|
@jakebailey Here are the results of running the user tests with tsc comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Everything looks good! |
|
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@jakebailey Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
|
The DT one implies that this shouldn't apply in declaration files. focus({ emitSynthFocused }?: {
emitSynthFocused: boolean;
}): Promise<void>; |
fixes #60119
fixes #60179