mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-19 19:59:23 -05:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user