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

Cannot delete from inferred types without typing them first #51340

Closed
gabriel-peracio opened this issue Oct 28, 2022 · 4 comments
Closed

Cannot delete from inferred types without typing them first #51340

gabriel-peracio opened this issue Oct 28, 2022 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@gabriel-peracio
Copy link

gabriel-peracio commented Oct 28, 2022

Bug Report

πŸ”Ž Search Terms

Cannot delete from inferred types without typing them first

πŸ•— Version & Regression Information

  • This changed between versions 3.9 and 4.0

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const someObject = {
    foo: 'bar',
    baz: true
}

delete someObject.foo // The operand of a 'delete' operator must be optional.(2790)

πŸ™ Actual behavior

You cannot delete from a literal object without facing the error.
You are forced to type the object, marking the property you want to delete as optional, even though it is not optional up until that point.

πŸ™‚ Expected behavior

Since you never provided a type to the object, you should be able to delete from it, and afterwards it gets marked as optional.
Alternatively, the check should be divorced from strictNullChecks so it can be turned off separately

@fatcerberus
Copy link

This is intentional. You also can’t assign new properties after the fact either.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 28, 2022
@RyanCavanaugh
Copy link
Member

This behavior was specifically requested; see #13783.

@fatcerberus
Copy link

fatcerberus commented Oct 28, 2022

@RyanCavanaugh fwiw 13783 involves explicit type annotations - the OP here has an inferred object literal and seems to be expecting it to act as an evolving type.

Maybe worth noting that there’s no way to declare a property as optional in an object literal without a type annotation…

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants