Narrow via discriminant property through optional chain (#42450)

* Naive attempt at narrowing via discriminant property through optional chain

* Clean up test
This commit is contained in:
Andrew Branch
2021-01-25 09:29:51 -08:00
committed by GitHub
parent 290af69b73
commit 89c173fddc
7 changed files with 149 additions and 8 deletions

View File

@@ -22599,10 +22599,13 @@ namespace ts {
if (propName === undefined) {
return type;
}
const propType = getTypeOfPropertyOfType(type, propName);
const includesUndefined = strictNullChecks && maybeTypeOfKind(type, TypeFlags.Undefined);
const removeOptional = includesUndefined && isOptionalChain(access);
let propType = getTypeOfPropertyOfType(removeOptional ? getTypeWithFacts(type, TypeFacts.NEUndefined) : type, propName);
if (!propType) {
return type;
}
propType = removeOptional ? getOptionalType(propType) : propType;
const narrowedPropType = narrowType(propType);
return filterType(type, t => {
const discriminantType = getTypeOfPropertyOrIndexSignature(t, propName);