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

"delete" does not change the type but only reports error #55682

Closed
kkmuffme opened this issue Sep 8, 2023 · 2 comments
Closed

"delete" does not change the type but only reports error #55682

kkmuffme opened this issue Sep 8, 2023 · 2 comments

Comments

@kkmuffme
Copy link

kkmuffme commented Sep 8, 2023

πŸ”Ž Search Terms

object delete TS2790 optional property

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries

⏯ Playground Link

https://www.typescriptlang.org/play?#code/FDBmFcDsGMBcEsD2kAEpGIBQoIYCcBzALhQCMMAbAUx1QEoUBvYFVlaZAZ1jXiooAmnFAF4mwAJASOUWHgCeJAOQ4lAGkkTuOWFWWqNU6PFiKUK9ZoAOibhwF7zBzTgEC8VTpwD6ARn2WEgC+ANwgbCjwoCiY+ASiImJy4FQMzBERDtS6vPxCAHSu7p4+vmERQeFsHJDcKAAeormCnGFBQA

πŸ’» Code

function foo( arg: boolean ) {
    const fields = {
		country: 'a',
		state: 'a',
		city: 'a',
		postcode: 'a',
		address_1: 'a',
	};

    if (arg === true) {
        delete fields.address_1;
    }

    const x = fields;
}

πŸ™ Actual behavior

address_1 in x is non-optional

πŸ™‚ Expected behavior

address_1 in x should be optional with ? since in some cases it is deleted.

Typescript reports an error for this, however that doesn't mean that delete doesn't change the type.
e.g. if errors are ignored for this, the type will be wrong and lead to bugs downstream.

Additional information about the issue

Followup to #13783

@fatcerberus
Copy link

if errors are ignored for this, the type will be wrong and lead to bugs downstream.

I mean, this is true of pretty much all TS errors, since that’s the entire point of the type checker. β€œIf you ts-ignore this error you might shoot yourself in the foot” isn’t really a good argument for changing the behavior.

@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 8, 2023

This sounds like a duplicate of #53697. Or more accurately #51340.

if errors are ignored for this, the type will be wrong and lead to bugs downstream.

That basically applies to all errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants