-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Remove key from object after delete object.key
#53697
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
Comments
A more clearly-stated example would be nice |
Sorry. I've added a new example with some comments after it; hope that clarifies things a bit more. |
I don't think this actually matters, regardless of |
Perhaps, but what I mostly would like to see is a type update where the key is omitted after the It does seem to me it will need to include Essentially, there are two situations to consider.
Narrowing In case of a type union, such as
If the situation is such, where "key" is optional, TypeScript will need to consider the possibility of the key being absent at the start.
Deleting the key entirely
|
nice |
Uh oh!
There was an error while loading. Please reload this page.
Suggestion
π Search Terms
delete
delete key
narrowing
narrowing delete
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
In TypeScript, it's already possible to have objects add keys to their types using
if ("key" in object) // object: { key: unknown }
. It is not currently possible to do the exact opposite: deleting a key off of an object.This could be achieved in two ways:
type { key: string }; delete object.key; type { }
.type { key?: string | undefined }; if (object.key === undefined) delete object.key; type { key?: string }
.The guaranteed way is pretty straight forward: it removes the key entirely.
The optional way is slightly more tricky: it should only remove the type information checked in the if statement. Remaining type options should still exist after the if statement. If the last remaining type option is removed, then it should function the same as the guaranteed way.
Naturally, if the key didn't exist on the object in the first place, the type information should remain the exact same as before.
π Motivating Example
I'm using the zod library, with which I set a value on object user as

{ givenName: string; infix?: string | undefined; surname: string; }
.I wrote a factory function
toUser({ givenName, infix, surname }: { givenName: string; infix?: string; surname: string; }): User { ... }
.The type
infix?: string | undefined
is not compatible with typeinfix?: string
.--- EDIT: Extra example ---
After executing
delete object.key
, I'm expecting the type information to no longer contain this key:Now, if I want to remove a key under certain conditions:
In the above example, the native types "number" and "string" get stripped off as expected. The type "undefined" however, does not. This breaks the tsconfig option exactOptionalPropertyTypes as the key should now no longer be able to exist. It cannot exist with literal value
undefined
.π» Use Cases
This is purely type information, but it seems essential if the typing system is to remain correct after the
delete
keyword.For the time being, I'll have to set the type to
infix?: string | undefined
, knowing it's not entirely accurate (key "infix" should not be permitted with value "undefined").The text was updated successfully, but these errors were encountered: