Skip to content

another way of noUncheckedIndexedAccess #41711

@LongTengDao

Description

@LongTengDao

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

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarifiedSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions