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