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

Add noUncheckedIndexedAccess to tsconfig for Stricter Index Access #77

Open
andylizf opened this issue Mar 7, 2024 · 0 comments · May be fixed by #78
Open

Add noUncheckedIndexedAccess to tsconfig for Stricter Index Access #77

andylizf opened this issue Mar 7, 2024 · 0 comments · May be fixed by #78
Labels
enhancement New feature or request

Comments

@andylizf
Copy link
Contributor

andylizf commented Mar 7, 2024

Description

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.

@andylizf andylizf added the enhancement New feature or request label Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
1 participant