-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[no-delete-optional] Prohibit deleting optional properties #1634
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
Here are some implementation suggestions: |
This is probably better suited to be built into the compiler itself. That being said, it wouldn't be hard to implement this as a lint rule, as long as |
Here are my considerations:
Do you have any suggestions on where to start? |
Correct, but TS releases breaking changes in minor bumps, so it would only be a few months until it gets released for everyone.
The TS maintainers wouldn't merge a PR if they weren't confident it won't break TS for anyone. They'll make sure you have enough test coverage. And then they merge something broken, it won't be you on the hook to fix it - it's then on the TS maintainers to fill in any gaps. Such is the burden of being an open source maintainer. An example of a breaking change I recently made to TS, which will be rolled out with v3.9 Note that your change will be available quickly in the nightly builds, and a month or so before release in the beta and then RC builds. So there's much time for the change to be validated and fixed if it needs to be. Happy to accept a PR if you want to implement this as a rule here first. |
I guess it should look something like this.
Am I going in the right direction? |
it looks pretty good. You can handle const property = memberExpression.property;
if (memberExpression.computed && property.type === AST_NODE_TYPES.Identifier) {
return true; // can't handle non-constant string property access
}
const name = property.type === AST_NODE_TYPES.Identifier ? property.name : property.value;
const objType = esTreeNodeToTSNodeMap.get(memberExpression.object);
const propType = objType.getProperty(name);
const propDecl = propType.getDeclarations();
return !isOptional(propDecl); |
It looks like it would be included in TS 4.0: |
Feature request
Expected Result
Should throw a compile error
deleting non-optional property 'a'
Actual Result
Runtime error
can't read property 'toString' of undefined
The text was updated successfully, but these errors were encountered: