chore(docs): use optional chaining rather than non-null assertion #10129
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While reading through the excellent documentation of
useFragment_experimental
, I noticed that the example usinguseQuery
uses the non-null assertion operator as a way to assert thatdata
can't be null in this context, which is unfortunately not the case.I think this usage could be confusing as the code snippet mentioned JavaScript, but that operator won't have any effects outside of TypeScript as the operator is simply removed in the emitted JavaScript code. Even with TypeScript, if someone copy-paste that example, it could lead to a runtime error are we're telling the compiler that
data
is a known non-null value, but we never asserted it was the case. If we omit it, then TypeScript will show the following error:Cannot read properties of undefined (reading 'list')
, but since we're using the non-null assertion operator, we're just muting the error, which is leading to a runtime error.I think it could make sense to use the non-null assertion operator combined with
strictNullChecks
, but only if we're making sure to assert it before! E.g.:The current code sample is failing with a runtime error with both JavaScript and TypeScript, and to remove some confusion for readers that could be wondering why they need the non-null assertion operator (post-fix
!
), I would suggest we simplify this by using the optional chaining operator (?.
) instead.cc @alessbell
Checklist: