-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Since 4.8.2, union discrimination fails with (string & {})
when noUncheckedIndexedAccess
is true
#50683
Comments
This seems to be the relevant difference:
|
Thanks for narrowing down the inferred type! You're right, type Alignment = (string & {}) | "left" | "center" | "right";
type Alignments = {
[key in Alignment]: string;
};
const a: Alignments = {
left: "align-left",
center: "align-center",
right: "align-right",
other: "align-other",
};
console.log(a.left.length); // Fine under 4.7.4. Error under 4.8.2
console.log(a.other.length); // Error for both versions (expected, however, in 4.8.2, I don't believe it's failing for the 'right reason') |
Not only unions. Here is a simpler example: function SendBlob(data: unknown, encoding: unknown) {
if (encoding !== undefined && encoding !== 'utf8') {
throw new Error('encoding');
}
// Mouse hover to see detected type
// When using TS v4.8.2 it is {} | undefined
// When using TS v4.7.4 it is "utf8" | undefined
encoding;
}; |
That’s… not even close to being the same problem as described here. |
While this is not in fact an issue relating to |
For the record, definitely related, now its own ticket: #50706 |
Bug Report
🔎 Search Terms
noUncheckedIndexedAccess
string & {}
union
intersection reduction
🕗 Version & Regression Information
4.8.2
(string & {})
should be an exceptionnoUncheckedIndexedAccess: true
⏯ Playground Link
Playground link with relevant code
💻 Code
tsconfig.json
🙁 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
The text was updated successfully, but these errors were encountered: