Skip to content

Commit e826824

Browse files
committed
Change fallback type from never to unknown
1 parent cca83dc commit e826824

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15261,27 +15261,29 @@ namespace ts {
1526115261
boolean. We know that number cannot be selected
1526215262
because it is caught in the first clause.
1526315263
*/
15264-
const isTypeUnknown = type.flags & TypeFlags.Unknown;
15265-
const isTypeAny = type.flags & TypeFlags.Any;
15266-
const getTypeFromName = (text: string) =>
15267-
(text === "function" && !isTypeAny) ? globalFunctionType :
15268-
(text === "object" && isTypeUnknown) ? getUnionType([nonPrimitiveType, nullType]) :
15269-
(typeofTypesByName.get(text) || neverType);
15264+
const getTypeFromName = (text: string) => {
15265+
switch (text) {
15266+
case "function":
15267+
return type.flags & TypeFlags.Any ? type : globalFunctionType;
15268+
case "object":
15269+
return type.flags & TypeFlags.Unknown ? getUnionType([nonPrimitiveType, nullType]) : type;
15270+
default:
15271+
return typeofTypesByName.get(text) || unknownType;
15272+
}
15273+
};
1527015274
if (!(hasDefaultClause || (type.flags & TypeFlags.Union))) {
1527115275
let impliedType = getTypeWithFacts(getUnionType(clauseWitnesses.map(getTypeFromName)), switchFacts);
1527215276
if (impliedType.flags & TypeFlags.Union) {
1527315277
impliedType = getAssignmentReducedType(impliedType as UnionType, getBaseConstraintOfType(type) || type);
1527415278
}
15275-
if (!(impliedType.flags & TypeFlags.Never)) {
15276-
if (isTypeSubtypeOf(impliedType, type)) {
15277-
// Intersection to handle `string` being a subtype of `keyof T`
15278-
return isTypeAny ? impliedType : getIntersectionType([type, impliedType]);
15279-
}
15280-
if (type.flags & TypeFlags.Instantiable) {
15281-
const constraint = getBaseConstraintOfType(type) || anyType;
15282-
if (isTypeSubtypeOf(impliedType, constraint)) {
15283-
return getIntersectionType([type, impliedType]);
15284-
}
15279+
if (isTypeSubtypeOf(impliedType, type)) {
15280+
// Intersection to handle `string` being a subtype of `keyof T`
15281+
return type.flags & TypeFlags.Any ? impliedType : getIntersectionType([type, impliedType]);
15282+
}
15283+
if (type.flags & TypeFlags.Instantiable) {
15284+
const constraint = getBaseConstraintOfType(type) || anyType;
15285+
if (isTypeSubtypeOf(impliedType, constraint)) {
15286+
return getIntersectionType([type, impliedType]);
1528515287
}
1528615288
}
1528715289
}

0 commit comments

Comments
 (0)