Skip to content

Commit 17f6e57

Browse files
committed
Revert removal of nonInferrableAnyType
1 parent 3644959 commit 17f6e57

File tree

3 files changed

+24
-52
lines changed

3 files changed

+24
-52
lines changed

src/compiler/checker.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ namespace ts {
791791
const wildcardType = createIntrinsicType(TypeFlags.Any, "any");
792792
const errorType = createIntrinsicType(TypeFlags.Any, "error");
793793
const unresolvedType = createIntrinsicType(TypeFlags.Any, "unresolved");
794+
const nonInferrableAnyType = createIntrinsicType(TypeFlags.Any, "any", ObjectFlags.ContainsWideningType);
794795
const intrinsicMarkerType = createIntrinsicType(TypeFlags.Any, "intrinsic");
795796
const unknownType = createIntrinsicType(TypeFlags.Unknown, "unknown");
796797
const nonNullUnknownType = createIntrinsicType(TypeFlags.Unknown, "unknown");
@@ -9560,7 +9561,11 @@ namespace ts {
95609561
if (reportErrors && !declarationBelongsToPrivateAmbientMember(element)) {
95619562
reportImplicitAny(element, anyType);
95629563
}
9563-
return anyType;
9564+
// When we're including the pattern in the type (an indication we're obtaining a contextual type), we
9565+
// use a non-inferrable any type. Inference will never directly infer this type, but it is possible
9566+
// to infer a type that contains it, e.g. for a binding pattern like [foo] or { foo }. In such cases,
9567+
// widening of the binding pattern type substitutes a regular any for the non-inferrable any.
9568+
return includePatternInType ? nonInferrableAnyType : anyType;
95649569
}
95659570

95669571
// Return the type implied by an object binding pattern
@@ -22631,7 +22636,10 @@ namespace ts {
2263122636
//
2263222637
// This flag is infectious; if we produce Box<never> (where never is silentNeverType), Box<never> is
2263322638
// also non-inferrable.
22634-
if (getObjectFlags(source) & ObjectFlags.NonInferrableType) {
22639+
//
22640+
// As a special case, also ignore nonInferrableAnyType, which is a special form of the any type
22641+
// used as a stand-in for binding elements when they are being inferred.
22642+
if (getObjectFlags(source) & ObjectFlags.NonInferrableType || source === nonInferrableAnyType) {
2263522643
return;
2263622644
}
2263722645
if (!inference.isFixed) {

tests/baselines/reference/issue50680.errors.txt

-36
This file was deleted.

tests/baselines/reference/issue50680.types

+14-14
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ function func1() {
1010
>func1 : () => void
1111

1212
const { firstKey } = func({keys: ["aa", "bb"]})
13-
>firstKey : string
14-
>func({keys: ["aa", "bb"]}) : { readonly keys: string[]; readonly firstKey: string; }
13+
>firstKey : "aa" | "bb"
14+
>func({keys: ["aa", "bb"]}) : { readonly keys: ("aa" | "bb")[]; readonly firstKey: "aa" | "bb"; }
1515
>func : <T extends string>(arg: { keys: T[]; }) => { readonly keys: T[]; readonly firstKey: T; }
16-
>{keys: ["aa", "bb"]} : { keys: string[]; }
17-
>keys : string[]
18-
>["aa", "bb"] : string[]
16+
>{keys: ["aa", "bb"]} : { keys: ("aa" | "bb")[]; }
17+
>keys : ("aa" | "bb")[]
18+
>["aa", "bb"] : ("aa" | "bb")[]
1919
>"aa" : "aa"
2020
>"bb" : "bb"
2121

2222
const a: "aa" | "bb" = firstKey;
2323
>a : "aa" | "bb"
24-
>firstKey : string
24+
>firstKey : "aa" | "bb"
2525

2626
const { keys } = func({keys: ["aa", "bb"]})
2727
>keys : ("aa" | "bb")[]
@@ -42,23 +42,23 @@ function func2() {
4242
>func2 : () => void
4343

4444
const { keys, firstKey } = func({keys: ["aa", "bb"]})
45-
>keys : string[]
46-
>firstKey : string
47-
>func({keys: ["aa", "bb"]}) : { readonly keys: string[]; readonly firstKey: string; }
45+
>keys : ("aa" | "bb")[]
46+
>firstKey : "aa" | "bb"
47+
>func({keys: ["aa", "bb"]}) : { readonly keys: ("aa" | "bb")[]; readonly firstKey: "aa" | "bb"; }
4848
>func : <T extends string>(arg: { keys: T[]; }) => { readonly keys: T[]; readonly firstKey: T; }
49-
>{keys: ["aa", "bb"]} : { keys: string[]; }
50-
>keys : string[]
51-
>["aa", "bb"] : string[]
49+
>{keys: ["aa", "bb"]} : { keys: ("aa" | "bb")[]; }
50+
>keys : ("aa" | "bb")[]
51+
>["aa", "bb"] : ("aa" | "bb")[]
5252
>"aa" : "aa"
5353
>"bb" : "bb"
5454

5555
const a: "aa" | "bb" = firstKey;
5656
>a : "aa" | "bb"
57-
>firstKey : string
57+
>firstKey : "aa" | "bb"
5858

5959
const b: ("aa" | "bb")[] = keys;
6060
>b : ("aa" | "bb")[]
61-
>keys : string[]
61+
>keys : ("aa" | "bb")[]
6262
}
6363

6464
function func3() {

0 commit comments

Comments
 (0)