-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
Bug Report
🔎 Search Terms
noUncheckedIndexedAccess
string & {}
union
intersection reduction
🕗 Version & Regression Information
4.8.2
- This changed between versions 4.7 and 4.8
- Possibly related to this PR, where
(string & {})
should be an exception - Only when
noUncheckedIndexedAccess: true
⏯ Playground Link
Playground link with relevant code
💻 Code
// TS 4.8.2
// This does not fail by default under strict mode, since noUncheckedIndexedAccess is false by default
// Switch noUncheckedIndexedAccess to true to see the error
// Then switch to 4.7.4 to see the error disappear
type Alignment = (string & {}) | "left" | "center" | "right";
type Alignments = Record<Alignment, string>;
const a: Alignments = {
left: "align-left",
center: "align-center",
right: "align-right",
other: "align-other",
};
console.log(a.left.length); // This shouldn't be considered as possibly "undefined", since "left" is a defined Alignment (incorrect behaviour under 4.8.2)
console.log(a.other.length); // This should be considered as possibly "undefined" (correct behaviour)
tsconfig.json
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"strict": true,
"noUncheckedIndexedAccess": true
}
}
🙁 Actual behavior
TypeScript 4.8.2 reports that an object's property is possibly "undefined", even for declared fields in the union.
🙂 Expected behavior
TypeScript 4.8.2 should only report that an object's property is possibly "undefined" for un-declared fields, as stated in the documentation
PointSingularity
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue