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

Enable noUncheckedIndexedAccess in tsconfig #9878

Open
amjithtitus09 opened this issue Jan 10, 2025 · 12 comments · May be fixed by #9880
Open

Enable noUncheckedIndexedAccess in tsconfig #9878

amjithtitus09 opened this issue Jan 10, 2025 · 12 comments · May be fixed by #9880
Labels
needs-triage question Further information is requested work-in-progress

Comments

@amjithtitus09
Copy link
Contributor

amjithtitus09 commented Jan 10, 2025

We need enable noUncheckedIndexedAccess in compilerOptions in tsconfig.json

Enabling this unveils quite a few TypeScript errors which would have to be solved as part of enabling this option. These would need to be resolved within the PR for it to be considered as an acceptable solution.

In order to get assigned, make the required changes in tsconfig, and mention your approach on how you would solve the errors, including an ETA on your expected turnaround time

@github-project-automation github-project-automation bot moved this to Triage in Care Jan 10, 2025
@amjithtitus09 amjithtitus09 moved this from Triage to Up Next in Care Jan 10, 2025
@Utkarsh-Anandani
Copy link

Utkarsh-Anandani commented Jan 10, 2025

can I work on this @amjithtitus09 @rithviknishad

@amjithtitus09
Copy link
Contributor Author

@Utkarsh-Anandani Check the updated Issue description regarding the process to get the issue assigned

@AdityaJ2305
Copy link
Contributor

AdityaJ2305 commented Jan 10, 2025

Hey @amjithtitus09 , I would like to work on this issue. As I would make "noUncheckedIndexedAccess": true and compile the code accordingly and manually fix the errors probably checking undefined or null before accessing the array indices
Time to complete : EOD

Image

After enabling it :
Image

Solution:
Image

@Rishith25
Copy link
Contributor

Rishith25 commented Jan 10, 2025

Hi @amjithtitus09
Enabled noUncheckedIndexedAccess in tsconfig.json to enforce stricter checks on indexed access.

Changes:

Added "noUncheckedIndexedAccess": true to compilerOptions.
Updated array/object access with null/undefined checks, type assertions, optional chaining, and default values where necessary.
Estimated resolution time for errors: by today.

@github-actions github-actions bot added needs-triage question Further information is requested labels Jan 10, 2025
@Vivekgupta008
Copy link

Vivekgupta008 commented Jan 10, 2025

Hey @amjithtitus09 ,
In the tsconfig.json file, enable the noUncheckedIndexedAccess option under the compilerOptions.

Enabling this option often leads to errors in the following cases:

Array Access: Accessing an array without null-checking or type guards.
Object Indexing: Accessing object properties using a key without ensuring the key exists.
Undefined Handling: Code assuming non-null or non-undefined values from indexing.

Approach to Resolve Errors

  1. Check for Undefined or Add Type Guards:
    Use optional chaining (?.) or nullish coalescing (??) operators to handle cases where an index might return undefined.

  2. Default Values:
    Provide a default value if an index might be undefined.

  3. Unit Tests:
    Add or update unit tests to ensure edge cases (e.g., accessing empty or undefined indexes) are handled correctly.

Before fixing it:
Image

After enabling and fixing errors:
Image

All the changes would be done latest before 12 midnight today (4 - 5 hours) . Please assign this task to me

@i0am0arunava
Copy link
Contributor

i0am0arunava commented Jan 10, 2025

@amjithtitus09
Problem:

  1. Array Index Access
    Accessing an array index directly will result in the inferred type being T | undefined, which requires handling undefined.

  2. Object Property Access
    Accessing an object property using a key that might not exist will infer the type as T | undefined, even if the key is not explicitly optional in the object type.

  3. Optional Properties
    If an object has optional properties, accessing them will require explicitly handling undefined.

  4. Dynamic Key Access
    When accessing properties of an object dynamically using a string or variable as the key, TypeScript will include undefined in the inferred type if the key might not exist.

  5. Iteration with Dynamic Indices
    Accessing elements in an array or object dynamically during loops or conditionally can raise errors if the index or key is not guaranteed to exist.

  6. Destructuring
    Destructuring properties or elements from an array or object will require explicit handling of cases where the property or index might not exist.

Solution:
1.Handle undefined Cases Explicitly
When accessing arrays or objects, TypeScript will infer the type as including undefined. To resolve this:

   - Use nullish coalescing (??) or logical OR (||) to provide default values.
   - Add checks to handle cases where the value might be undefined.

2.Use Type Narrowing
Use conditional statements or TypeScript's type narrowing to check for the existence of a value.
3. Update Types to Reflect Reality
Refactor your type definitions to include undefined explicitly if the value is genuinely optional. This ensures that TypeScript's strict checks align with your actual data.
4. Use Safe Iteration
When working with arrays, use Array.prototype.map, filter, or other methods to avoid directly accessing indices.
5. Leverage Utility Functions
Create reusable utility functions to safely access array elements or object properties.

Here I fixed an error by a simple change and as I have noticed most of the error will be fixed using this 5 solution
Image

will fix these all type error by Today (5 to 6 hours), Please assign this task to me

@Rishith25
Copy link
Contributor

Rishith25 commented Jan 10, 2025

@amjithtitus09

  1. I enabled noUncheckedIndexedAccess in tsconfig.json fileafter enabling it. It shows the errors found in the following list of files and i have done one change in one of the file before and after

Image

Before After
Image

if this is correct Please assign this to me. I will make the PR by 6 or 7 PM by today

@AnveshNalimela
Copy link
Contributor

AnveshNalimela commented Jan 10, 2025

hi @amjithtitus09
Enabling the noUncheckedIndexedAccess option under the compilerOptions.
there are around 161 Erros . Mostly of them resloved with small changes like null safety and data type checkings

Most of erros are

  • Undefined values: Many of the errors occur because TypeScript is unable to guarantee that a value is not undefined.

  • Type mismatches: Some errors arise from passing a value of type undefined when TypeScript expects a specific type.

  • Indexing issues: A few errors stem from trying to access properties on potentially undefined objects or arrays.

  • State updates: There are a few issues related to updating state with potentially undefined values.

  • Event handlers and file operations: Errors related to file handling or event handler callbacks may arise when the files or event objects are potentially undefined

They are slove through following pratices.

Use optional chaining (?.) to safely access properties of potentially undefined objects.
Use nullish coalescing (??) to provide fallback values when a value is undefined.
Explicitly check for undefined using conditionals (if statements) before using variables.
Ensure correct types are passed to functions and state setters by validating the data types.

Image
Image

I will Complete the PR By tonight 10 PM

@Utkarsh-Anandani
Copy link

Enable noUncheckedIndexedAccess in compilerOptions in tsconfig.json file enables errors while accessing data structures like array or object, the main errors are due to :

  • Array Access: Accessing an array without null-checking or type guards.
  • Object Indexing: Accessing object properties using a key without ensuring the key exists.
  • Undefined Handling: Code assuming non-null or non-undefined values from indexing.

The following errors can be fixed by using basic if else and optional checking methods.
The changes can be done by today EOD

@Rishith25 Rishith25 linked a pull request Jan 10, 2025 that will close this issue
6 tasks
@Rishith25
Copy link
Contributor

@amjithtitus09 I have raised the PR let me know if any changes are required

@AdityaJ2305
Copy link
Contributor

Hey @Rishith25 , I don't think this was supposed to be assigned on the basis of who first creates PR. We all actually waiting for final assignment result. I had also started working but would only raise PR if it gets assigned to me 😕

@Rishith25
Copy link
Contributor

Hey @Rishith25 , I don't think this was supposed to be assigned on the basis of who first creates PR. We all actually waiting for final assignment result. I had also started working but would only raise PR if it gets assigned to me 😕

I have asked it in the slack and Gigin told me to raise the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage question Further information is requested work-in-progress
Projects
Status: Up Next
Development

Successfully merging a pull request may close this issue.

7 participants