Fix discriminant property narrowing through optional chain with null (#42503)

* Fix discriminant property narrowing through optional chain with null

* Accept baselines

* Add tests from Anders
This commit is contained in:
Andrew Branch
2021-01-29 12:03:42 -08:00
committed by GitHub
parent 65e4d60d81
commit c15f40abfa
5 changed files with 660 additions and 4 deletions

View File

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