Skip to content

Assume that array lookup can return undefined inside conditional expressions #41872

@ifeltsweet

Description

@ifeltsweet

Search Terms

Array lookup
Array lookup undefined
Array indexing undefined

Suggestion

Please forgive my TypeScript terminology.

I am proposing for the compiler to assume that array lookup can return undefined inside conditional expressions ONLY.

I am NOT proposing for all array lookup to return T | undefined (#11122)

Use Cases

This helps to catch subtle, hard to spot bugs.

Examples

const setItem = (value: Item | undefined) => { item = value };

// This a bug, setItem shouldn't accept null, TS didn't catch it.
setItem(response.data.items[0] || null)

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

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions