Suggestion: option to include undefined in index signatures
Problem
Update: fixed by `--noUncheckedIndexedAccess` in TypeScript 4.1 --- Update: for my latest proposal see comment https://github.com/Microsoft/TypeScript/issues/13778#issuecomment-406316164 With `strictNullChecks` enabled, TypeScript does not include `undefined` in index signatures (e.g. on an object or array). This is a well known caveat and discussed in several issues, namely https://github.com/Microsoft/TypeScript/issues/9235, https://github.com/Microsoft/TypeScript/issues/13161, https://github.com/Microsoft/TypeScript/issues/12287, and https://github.com/Microsoft/TypeScript/pull/7140#issuecomment-192606629. Example: [code block] However, it appears from reading the above issues that many TypeScript users wish this wasn't the case. Granted, if index signatures did include `undefined`, code will likely require much more guarding, but—for some—this is an acceptable trade off for increased type safety. Example of index signatures including `undefined`: [code block] I would like to know whether this behaviour could be considered as an extra compiler option on top of `strictNullChecks`. This way, we are able to satisfy all groups of users: those who want strict null checks with or without undefined in their index signatures.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Enable Undefined in Index Signatures with Compiler Option
In TypeScript, when `strictNullChecks` is enabled, index signatures do not include `undefined` by default. This behavior is designed to enforce stricter type safety, but it has been noted that many users prefer the option to include `undefined` in their index signatures for greater flexibility in type definitions. The introduction of the `--noUncheckedIndexedAccess` flag in TypeScript 4.1 allows for this behavior, but it is not enabled by default.
Awaiting Verification
Be the first to verify this fix
- 1
Update TypeScript Configuration
Modify your TypeScript configuration file (tsconfig.json) to include the `noUncheckedIndexedAccess` option. This will allow index signatures to include `undefined` in their types.
json{ "compilerOptions": { "strictNullChecks": true, "noUncheckedIndexedAccess": true } } - 2
Adjust Type Definitions
Review and adjust any existing type definitions that utilize index signatures to account for the inclusion of `undefined`. This may require adding additional checks in your code to handle cases where the value could be `undefined`.
typescriptinterface Example { [key: string]: string | undefined; } - 3
Test Code for Undefined Handling
Run your TypeScript code and ensure that all instances where index signatures are used properly handle the potential for `undefined`. This may involve adding guards or default values where necessary.
typescriptconst example: Example = {}; const value = example['key'] || 'default'; console.log(value); // Should handle undefined gracefully - 4
Review and Refactor Code
After testing, review your codebase for any areas that may require refactoring due to the new handling of `undefined`. Ensure that all code paths are correctly managing the potential for undefined values.
typescript// Example refactor if (example['key'] !== undefined) { console.log(example['key']); }
Validation
To confirm the fix worked, compile your TypeScript project and ensure there are no type errors related to index signatures. Additionally, run unit tests to verify that all code paths handle `undefined` correctly.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep