-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
Needs More InfoThe issue still hasn't been fully clarifiedThe issue still hasn't been fully clarifiedSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Search Terms
Suggestion
tsconfig: noUncheckedIndexedAccess is a good new feature, but whole project level setting is too coarse-grained to use.
traditional { [index :number] :undefined | T } could control that in api level. but not like noUncheckedIndexedAccess, it makes arr[0] = undefined valid which is not intended.
I wait the feature so long, but when I open noUncheckedIndexedAccess, there are too many too many too many errors, which cause trouble far more than help.
so in fact, we need a feature that makes T-only when writing, and T | undefined when reading.
Use Cases
Examples
maybe something like:
type T = string;
const myArray :{
optional [index :number] :T
optional item :T
};
myArray[0]// T | undefined
myArray.item// T | undefined
myArray.item = undefined// error, undefined is readonly
if ( 'item' in myArray ) {
myArray.item// T
}
? to be rethinked would be best, but it took too much historical burden.
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Metadata
Metadata
Assignees
Labels
Needs More InfoThe issue still hasn't been fully clarifiedThe issue still hasn't been fully clarifiedSuggestionAn idea for TypeScriptAn idea for TypeScript