Skip to content

Commit 92bf91d

Browse files
committed
Reconcile fix with CFA work
1 parent a2feb0e commit 92bf91d

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7555,7 +7555,7 @@ namespace ts {
75557555
}
75567556

75577557
function getNarrowedTypeOfReference(type: Type, reference: Node) {
7558-
if (!(type.flags & TypeFlags.Narrowable) || !isNarrowableReference(reference)) {
7558+
if (type.flags & TypeFlags.Defaultable || !isNarrowableReference(reference)) {
75597559
return type;
75607560
}
75617561
const leftmostNode = getLeftmostIdentifierOrThis(reference);
@@ -7975,7 +7975,7 @@ namespace ts {
79757975
const defaultsToDeclaredType = !strictNullChecks || type.flags & TypeFlags.Any || !declaration ||
79767976
declaration.kind === SyntaxKind.Parameter || isInAmbientContext(declaration) ||
79777977
getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node);
7978-
if (defaultsToDeclaredType && !(type.flags & TypeFlags.Narrowable)) {
7978+
if (defaultsToDeclaredType && type.flags & TypeFlags.Defaultable) {
79797979
return type;
79807980
}
79817981
const flowType = getFlowTypeOfReference(node, type, defaultsToDeclaredType ? type : undefinedType);

src/compiler/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,13 @@ namespace ts {
21752175
ObjectType = Class | Interface | Reference | Tuple | Anonymous,
21762176
UnionOrIntersection = Union | Intersection,
21772177
StructuredType = ObjectType | Union | Intersection,
2178-
Narrowable = Any | ObjectType | Union | TypeParameter,
2178+
2179+
// 'Defaultable' types are types where narrowing reverts to the original type, rather than actually narrow.
2180+
// This is never really correct - you can _always_ narrow to an intersection with that type, _but_ we keep
2181+
// Void as the only defaultable type, since it's a non-value type construct (representing a lack of a value)
2182+
// and, generally speaking, narrowing `void` should fail in some way, as it is nonsensical. (`void` narrowing
2183+
// to `void & T`, in a structural sense, is just narrowing to T, which we wouldn't allow under normal rules)
2184+
Defaultable = Void,
21792185
/* @internal */
21802186
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral,
21812187
/* @internal */

0 commit comments

Comments
 (0)