You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In order to enhance the safety and reliability of our TypeScript codebase, I propose that we add the "noUncheckedIndexedAccess": true option to our tsconfig.json. This strict check will help prevent potential runtime errors caused by undefined index access in arrays or objects.
Motivation
Recently, a bug related to an undefined index access was encountered when @ziyebuxiu attempted to access an element in a singleton array using an index. Here is the scenario that illustrates the issue:
We have an array questionId that is expected to be a singleton array.
A member tried accessing the second element with questionId[1], which is undefined.
This led to a BadRequestException: Validation failed (numeric string is expected) error, because the API expected a numeric string, but received undefined due to the out-of-bounds access.
Suggested Solution
To avoid such errors and enforce better type checking, we can use the "noUncheckedIndexedAccess": true option, which will make sure that every property access includes undefined in the type if there's no explicit index signature.
I believe this change will encourage our developers to write more robust code and perform the necessary checks before accessing array indexes, ultimately leading to a more stable application.
The text was updated successfully, but these errors were encountered:
Description
In order to enhance the safety and reliability of our TypeScript codebase, I propose that we add the
"noUncheckedIndexedAccess": true
option to ourtsconfig.json
. This strict check will help prevent potential runtime errors caused byundefined
index access in arrays or objects.Motivation
Recently, a bug related to an
undefined
index access was encountered when @ziyebuxiu attempted to access an element in a singleton array using an index. Here is the scenario that illustrates the issue:questionId
that is expected to be a singleton array.questionId[1]
, which isundefined
.BadRequestException: Validation failed (numeric string is expected)
error, because the API expected a numeric string, but receivedundefined
due to the out-of-bounds access.Suggested Solution
To avoid such errors and enforce better type checking, we can use the "noUncheckedIndexedAccess": true option, which will make sure that every property access includes undefined in the type if there's no explicit index signature.
I believe this change will encourage our developers to write more robust code and perform the necessary checks before accessing array indexes, ultimately leading to a more stable application.
The text was updated successfully, but these errors were encountered: