Skip to content

Check index array length #38000

@mdbetancourt

Description

@mdbetancourt

Search Terms

Indexing, Array, Strict check

Suggestion

Strict check to indexing a array

Use Cases

Safety with arrays usages

Examples

const arr: House[] = .... // same as [] | House[]
arr[0] // throw error
if(arr.length > 0) {
  arr[0] // OK
  arr[200] // throw error
}
const arr2: [House, House] = ...
arr[0] // OK
arr[1] // OK

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code (with a flag)
  • 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

    SuggestionAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it adds

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions