-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
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
Labels
DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript