-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add noUncheckedIndexedAccess TypeScript compiler flag #84
Conversation
This seems good, but I'm wondering if waiting on TypeScript v4.4 would make this easier 🤔 Because of the better handling of unset v.s. |
Hm, I suppose we can wait two weeks, although I haven't considered how much easier it'll be. This comment from the PR that implements |
I was hoping this would let us avoid the null assertion operator in some of the cases where this change forced its introduction. Null assertions are escape hatches that we should try to avoid if possible. If we start sprinkling them everywhere for this rule, we likely won't have reason to come back to them anytime soon. |
I am not sure how I feel about this. I mean, I understand the root problem here, but I don't mind how TypeScript has solved this problem. |
This seems like a pretty clear case of there being a complete lack of type safety when it comes to index access, with this flag turning it back on. I don't understand what you mean about liking how TypeScript has solved this problem absent this flag... what problem are you referring to? How is it solved? |
@Gudahtt Sorry, I wasn't as accurate with my wording as I could have been. I was going to respond with something that emphasized the caveat that Erik mentioned — that I am concerned that this would add obstacles to both writing TypeScript and reading TypeScript — in that we would need to make a decision about what is real on behalf of TypeScript, and communicate the reasons we are overriding TypeScript in this way — which I feel may be unnecessary (practically, not theoretically, based on the code we tend to write). But after reviewing the violations that this would cause in So I've changed my mind on this. I regret that we have to do this, but it seems more beneficial than not. |
I should clarify that I expect most of the caveats mentioned by Erik will no longer be a problem after the I would prefer that we not merge this until after the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! exactOptionalPropertyTypes
has been enabled so I don't have any more reservations about this change introducing too much friction. Though I guess we'll see.
This PR adds the
noUncheckedIndexedAccess
compiler option totsconfig.json
. This flag forces us to check if the values of index signature keys are defined before working with them. For details, see thetsc
documentation.It is no exaggeration to say that much of our object type safety is mere theater without this flag. Without it, the following is valid TypeScript:
I advocate that we end this insanity now, and enable
noUncheckedIndexedAccess
everywhere. Unsurprisingly, our controllers contain the most violations of this rule, but only about 60 all in all: MetaMask/core#554.The main drawback of enabling this flag is that we have to add non-null assertions or unnecessary if statements to some parts of our code, because TypeScript generally can't map enumerated keys to their objects. For example:
However, trading convenience for type safety is the value proposition of TypeScript, so that's life, I guess.
Fixing the above line and keeping our linter happy is as simple as: