Allow discrimination to identical object types when discriminating contextual types (#40574)

* Merge identical object types when discriminating contextual types

Co-authored-by: Orta <ortam@microsoft.com>

* Allow identical discriminants when discriminating, rather than trying to unify identical union members

* Fix lint

Co-authored-by: Orta <ortam@microsoft.com>
This commit is contained in:
Wesley Wigham
2020-09-23 00:51:14 -07:00
committed by GitHub
parent ad2a07440c
commit a91c2879ef
6 changed files with 159 additions and 2 deletions

View File

@@ -18348,8 +18348,18 @@ namespace ts {
}
}
const match = discriminable.indexOf(/*searchElement*/ true);
if (match === -1) {
return defaultValue;
}
// make sure exactly 1 matches before returning it
return match === -1 || discriminable.indexOf(/*searchElement*/ true, match + 1) !== -1 ? defaultValue : target.types[match];
let nextMatch = discriminable.indexOf(/*searchElement*/ true, match + 1);
while (nextMatch !== -1) {
if (!isTypeIdenticalTo(target.types[match], target.types[nextMatch])) {
return defaultValue;
}
nextMatch = discriminable.indexOf(/*searchElement*/ true, nextMatch + 1);
}
return target.types[match];
}
/**

View File

@@ -4170,7 +4170,7 @@ namespace ts {
export const enum UnionReduction {
None = 0,
Literal,
Subtype
Subtype,
}
/* @internal */