Skip to content
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

Issue a better error for exactOptionalPropertyTypes #44403

Closed
DanielRosenwasser opened this issue Jun 3, 2021 · 1 comment · Fixed by #45032
Closed

Issue a better error for exactOptionalPropertyTypes #44403

DanielRosenwasser opened this issue Jun 3, 2021 · 1 comment · Fixed by #45032
Assignees
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue

Comments

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jun 3, 2021

A project under --strict will likely break in TypeScript 4.4, and it won't be obvious why.

foo/thing.ts:37:5 - error TS2322: Type 'SomeData | undefined' is not assignable to type 'SomeData'.
  Type 'undefined' is not assignable to type 'SomeData'.

37     data: data,
       ~~~~

What I would propose is that we give some better error message in these cases such as

The source property type 'SomeData | undefined' is not compatible with the target property type 'SomeData' under '--strictOptionalProperties'. Consider adding 'undefined' to the type of the target property.

This could then be given a quick fix.

We could give this error when

  • The source property is optional and the target type includes undefinedness
  • The types would otherwise be compatible if you were to remove the undefined from the target.
@DanielRosenwasser DanielRosenwasser added Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements labels Jun 3, 2021
@sandersn sandersn changed the title Issue a better error for strictOptionalProperties Issue a better error for eactOptionalPropertyTypes Jul 8, 2021
@sandersn sandersn changed the title Issue a better error for eactOptionalPropertyTypes Issue a better error for exactOptionalPropertyTypes Jul 8, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Jul 14, 2021
@DanielRosenwasser
Copy link
Member Author

@simeyla has also filed #45058 where, if directly assigning an undefined to a property, we could potentially suggest a delete operator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants