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

Type safe for Reflect.deleteProperty(object, string|number|symbol) #186

Open
zanminkian opened this issue Feb 5, 2024 · 2 comments
Open

Comments

@zanminkian
Copy link

Reflect.deleteProperty is equivalent to delete operator. Reflect.deleteProperty is modern, but not type safe enough.

@zanminkian
Copy link
Author

Example

const obj = {
  a: 123
}

delete obj.b // type error
Reflect.deleteProperty(obj, 'b') // pass

@zanminkian
Copy link
Author

Adding restriction to function parameters is difficult.

// reflect-delete-property.d.ts
declare namespace Reflect {
  /**
   * Removes a property from an object, equivalent to `delete target[propertyKey]`,
   * except it won't throw if `target[propertyKey]` is non-configurable.
   * @param target Object from which to remove the own property.
   * @param propertyKey The property name.
   */
    function deleteProperty<T extends object>(target: T, propertyKey: keyof T): boolean;
}

// other-file.ts
const a = {b:123};
Reflect.deleteProperty(a, 'c'); // no compiling error 🙁

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

1 participant