Skip to content

Commit cca83dc

Browse files
committed
Don't narrow any to Function
1 parent fe78b43 commit cca83dc

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15262,9 +15262,11 @@ namespace ts {
1526215262
because it is caught in the first clause.
1526315263
*/
1526415264
const isTypeUnknown = type.flags & TypeFlags.Unknown;
15265-
const getTypeFromName = (text: string) => text === "function" ? globalFunctionType :
15266-
(isTypeUnknown && text === "object") ? getUnionType([nonPrimitiveType, nullType]) :
15267-
(typeofTypesByName.get(text) || neverType);
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);
1526815270
if (!(hasDefaultClause || (type.flags & TypeFlags.Union))) {
1526915271
let impliedType = getTypeWithFacts(getUnionType(clauseWitnesses.map(getTypeFromName)), switchFacts);
1527015272
if (impliedType.flags & TypeFlags.Union) {
@@ -15273,7 +15275,7 @@ namespace ts {
1527315275
if (!(impliedType.flags & TypeFlags.Never)) {
1527415276
if (isTypeSubtypeOf(impliedType, type)) {
1527515277
// Intersection to handle `string` being a subtype of `keyof T`
15276-
return isTypeAny(type) ? impliedType : getIntersectionType([type, impliedType]);
15278+
return isTypeAny ? impliedType : getIntersectionType([type, impliedType]);
1527715279
}
1527815280
if (type.flags & TypeFlags.Instantiable) {
1527915281
const constraint = getBaseConstraintOfType(type) || anyType;

tests/baselines/reference/narrowingByTypeofInSwitch.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ function unknownNarrowing(x: unknown) {
228228
}
229229

230230
function keyofNarrowing<S extends { [K in keyof S]: string }>(k: keyof S) {
231-
function assertKeyofT(k1: keyof S) { }
231+
function assertKeyofS(k1: keyof S) { }
232232
switch (typeof k) {
233-
case 'number': assertNumber(k); assertKeyofT(k); return;
234-
case 'symbol': assertSymbol(k); assertKeyofT(k); return;
235-
case 'string': assertString(k); assertKeyofT(k); return;
233+
case 'number': assertNumber(k); assertKeyofS(k); return;
234+
case 'symbol': assertSymbol(k); assertKeyofS(k); return;
235+
case 'string': assertString(k); assertKeyofS(k); return;
236236
}
237237
}
238238

@@ -524,19 +524,19 @@ function unknownNarrowing(x) {
524524
}
525525
}
526526
function keyofNarrowing(k) {
527-
function assertKeyofT(k1) { }
527+
function assertKeyofS(k1) { }
528528
switch (typeof k) {
529529
case 'number':
530530
assertNumber(k);
531-
assertKeyofT(k);
531+
assertKeyofS(k);
532532
return;
533533
case 'symbol':
534534
assertSymbol(k);
535-
assertKeyofT(k);
535+
assertKeyofS(k);
536536
return;
537537
case 'string':
538538
assertString(k);
539-
assertKeyofT(k);
539+
assertKeyofS(k);
540540
return;
541541
}
542542
}

tests/baselines/reference/narrowingByTypeofInSwitch.symbols

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -642,30 +642,30 @@ function keyofNarrowing<S extends { [K in keyof S]: string }>(k: keyof S) {
642642
>k : Symbol(k, Decl(narrowingByTypeofInSwitch.ts, 228, 62))
643643
>S : Symbol(S, Decl(narrowingByTypeofInSwitch.ts, 228, 24))
644644

645-
function assertKeyofT(k1: keyof S) { }
646-
>assertKeyofT : Symbol(assertKeyofT, Decl(narrowingByTypeofInSwitch.ts, 228, 75))
645+
function assertKeyofS(k1: keyof S) { }
646+
>assertKeyofS : Symbol(assertKeyofS, Decl(narrowingByTypeofInSwitch.ts, 228, 75))
647647
>k1 : Symbol(k1, Decl(narrowingByTypeofInSwitch.ts, 229, 26))
648648
>S : Symbol(S, Decl(narrowingByTypeofInSwitch.ts, 228, 24))
649649

650650
switch (typeof k) {
651651
>k : Symbol(k, Decl(narrowingByTypeofInSwitch.ts, 228, 62))
652652

653-
case 'number': assertNumber(k); assertKeyofT(k); return;
653+
case 'number': assertNumber(k); assertKeyofS(k); return;
654654
>assertNumber : Symbol(assertNumber, Decl(narrowingByTypeofInSwitch.ts, 2, 1))
655655
>k : Symbol(k, Decl(narrowingByTypeofInSwitch.ts, 228, 62))
656-
>assertKeyofT : Symbol(assertKeyofT, Decl(narrowingByTypeofInSwitch.ts, 228, 75))
656+
>assertKeyofS : Symbol(assertKeyofS, Decl(narrowingByTypeofInSwitch.ts, 228, 75))
657657
>k : Symbol(k, Decl(narrowingByTypeofInSwitch.ts, 228, 62))
658658

659-
case 'symbol': assertSymbol(k); assertKeyofT(k); return;
659+
case 'symbol': assertSymbol(k); assertKeyofS(k); return;
660660
>assertSymbol : Symbol(assertSymbol, Decl(narrowingByTypeofInSwitch.ts, 14, 1))
661661
>k : Symbol(k, Decl(narrowingByTypeofInSwitch.ts, 228, 62))
662-
>assertKeyofT : Symbol(assertKeyofT, Decl(narrowingByTypeofInSwitch.ts, 228, 75))
662+
>assertKeyofS : Symbol(assertKeyofS, Decl(narrowingByTypeofInSwitch.ts, 228, 75))
663663
>k : Symbol(k, Decl(narrowingByTypeofInSwitch.ts, 228, 62))
664664

665-
case 'string': assertString(k); assertKeyofT(k); return;
665+
case 'string': assertString(k); assertKeyofS(k); return;
666666
>assertString : Symbol(assertString, Decl(narrowingByTypeofInSwitch.ts, 10, 1))
667667
>k : Symbol(k, Decl(narrowingByTypeofInSwitch.ts, 228, 62))
668-
>assertKeyofT : Symbol(assertKeyofT, Decl(narrowingByTypeofInSwitch.ts, 228, 75))
668+
>assertKeyofS : Symbol(assertKeyofS, Decl(narrowingByTypeofInSwitch.ts, 228, 75))
669669
>k : Symbol(k, Decl(narrowingByTypeofInSwitch.ts, 228, 62))
670670
}
671671
}

tests/baselines/reference/narrowingByTypeofInSwitch.types

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ function testAny(x: any) {
235235
>'function' : "function"
236236
>assertFunction(x) : Function
237237
>assertFunction : (x: Function) => Function
238-
>x : Function
238+
>x : any
239239

240240
case 'symbol': assertSymbol(x); return;
241241
>'symbol' : "symbol"
@@ -771,39 +771,39 @@ function keyofNarrowing<S extends { [K in keyof S]: string }>(k: keyof S) {
771771
>keyofNarrowing : <S extends { [K in keyof S]: string; }>(k: keyof S) => void
772772
>k : keyof S
773773

774-
function assertKeyofT(k1: keyof S) { }
775-
>assertKeyofT : (k1: keyof S) => void
774+
function assertKeyofS(k1: keyof S) { }
775+
>assertKeyofS : (k1: keyof S) => void
776776
>k1 : keyof S
777777

778778
switch (typeof k) {
779779
>typeof k : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
780780
>k : keyof S
781781

782-
case 'number': assertNumber(k); assertKeyofT(k); return;
782+
case 'number': assertNumber(k); assertKeyofS(k); return;
783783
>'number' : "number"
784784
>assertNumber(k) : number
785785
>assertNumber : (x: number) => number
786786
>k : keyof S & number
787-
>assertKeyofT(k) : void
788-
>assertKeyofT : (k1: keyof S) => void
787+
>assertKeyofS(k) : void
788+
>assertKeyofS : (k1: keyof S) => void
789789
>k : keyof S & number
790790

791-
case 'symbol': assertSymbol(k); assertKeyofT(k); return;
791+
case 'symbol': assertSymbol(k); assertKeyofS(k); return;
792792
>'symbol' : "symbol"
793793
>assertSymbol(k) : symbol
794794
>assertSymbol : (x: symbol) => symbol
795795
>k : keyof S & symbol
796-
>assertKeyofT(k) : void
797-
>assertKeyofT : (k1: keyof S) => void
796+
>assertKeyofS(k) : void
797+
>assertKeyofS : (k1: keyof S) => void
798798
>k : keyof S & symbol
799799

800-
case 'string': assertString(k); assertKeyofT(k); return;
800+
case 'string': assertString(k); assertKeyofS(k); return;
801801
>'string' : "string"
802802
>assertString(k) : string
803803
>assertString : (x: string) => string
804804
>k : keyof S & string
805-
>assertKeyofT(k) : void
806-
>assertKeyofT : (k1: keyof S) => void
805+
>assertKeyofS(k) : void
806+
>assertKeyofS : (k1: keyof S) => void
807807
>k : keyof S & string
808808
}
809809
}

0 commit comments

Comments
 (0)