Skip to content

Commit

Permalink
Revert removal of nonInferrableAnyType
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Sep 8, 2022
1 parent 3644959 commit 17f6e57
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 52 deletions.
12 changes: 10 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ namespace ts {
const wildcardType = createIntrinsicType(TypeFlags.Any, "any");
const errorType = createIntrinsicType(TypeFlags.Any, "error");
const unresolvedType = createIntrinsicType(TypeFlags.Any, "unresolved");
const nonInferrableAnyType = createIntrinsicType(TypeFlags.Any, "any", ObjectFlags.ContainsWideningType);
const intrinsicMarkerType = createIntrinsicType(TypeFlags.Any, "intrinsic");
const unknownType = createIntrinsicType(TypeFlags.Unknown, "unknown");
const nonNullUnknownType = createIntrinsicType(TypeFlags.Unknown, "unknown");
Expand Down Expand Up @@ -9560,7 +9561,11 @@ namespace ts {
if (reportErrors && !declarationBelongsToPrivateAmbientMember(element)) {
reportImplicitAny(element, anyType);
}
return anyType;
// When we're including the pattern in the type (an indication we're obtaining a contextual type), we
// use a non-inferrable any type. Inference will never directly infer this type, but it is possible
// to infer a type that contains it, e.g. for a binding pattern like [foo] or { foo }. In such cases,
// widening of the binding pattern type substitutes a regular any for the non-inferrable any.
return includePatternInType ? nonInferrableAnyType : anyType;
}

// Return the type implied by an object binding pattern
Expand Down Expand Up @@ -22631,7 +22636,10 @@ namespace ts {
//
// This flag is infectious; if we produce Box<never> (where never is silentNeverType), Box<never> is
// also non-inferrable.
if (getObjectFlags(source) & ObjectFlags.NonInferrableType) {
//
// As a special case, also ignore nonInferrableAnyType, which is a special form of the any type
// used as a stand-in for binding elements when they are being inferred.
if (getObjectFlags(source) & ObjectFlags.NonInferrableType || source === nonInferrableAnyType) {
return;
}
if (!inference.isFixed) {
Expand Down
36 changes: 0 additions & 36 deletions tests/baselines/reference/issue50680.errors.txt

This file was deleted.

28 changes: 14 additions & 14 deletions tests/baselines/reference/issue50680.types
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ function func1() {
>func1 : () => void

const { firstKey } = func({keys: ["aa", "bb"]})
>firstKey : string
>func({keys: ["aa", "bb"]}) : { readonly keys: string[]; readonly firstKey: string; }
>firstKey : "aa" | "bb"
>func({keys: ["aa", "bb"]}) : { readonly keys: ("aa" | "bb")[]; readonly firstKey: "aa" | "bb"; }
>func : <T extends string>(arg: { keys: T[]; }) => { readonly keys: T[]; readonly firstKey: T; }
>{keys: ["aa", "bb"]} : { keys: string[]; }
>keys : string[]
>["aa", "bb"] : string[]
>{keys: ["aa", "bb"]} : { keys: ("aa" | "bb")[]; }
>keys : ("aa" | "bb")[]
>["aa", "bb"] : ("aa" | "bb")[]
>"aa" : "aa"
>"bb" : "bb"

const a: "aa" | "bb" = firstKey;
>a : "aa" | "bb"
>firstKey : string
>firstKey : "aa" | "bb"

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

const { keys, firstKey } = func({keys: ["aa", "bb"]})
>keys : string[]
>firstKey : string
>func({keys: ["aa", "bb"]}) : { readonly keys: string[]; readonly firstKey: string; }
>keys : ("aa" | "bb")[]
>firstKey : "aa" | "bb"
>func({keys: ["aa", "bb"]}) : { readonly keys: ("aa" | "bb")[]; readonly firstKey: "aa" | "bb"; }
>func : <T extends string>(arg: { keys: T[]; }) => { readonly keys: T[]; readonly firstKey: T; }
>{keys: ["aa", "bb"]} : { keys: string[]; }
>keys : string[]
>["aa", "bb"] : string[]
>{keys: ["aa", "bb"]} : { keys: ("aa" | "bb")[]; }
>keys : ("aa" | "bb")[]
>["aa", "bb"] : ("aa" | "bb")[]
>"aa" : "aa"
>"bb" : "bb"

const a: "aa" | "bb" = firstKey;
>a : "aa" | "bb"
>firstKey : string
>firstKey : "aa" | "bb"

const b: ("aa" | "bb")[] = keys;
>b : ("aa" | "bb")[]
>keys : string[]
>keys : ("aa" | "bb")[]
}

function func3() {
Expand Down

0 comments on commit 17f6e57

Please sign in to comment.