Skip to content

Breaking change to extends any causes confusing behavior with indirect references to any #38555

Closed
@webstrand

Description

@webstrand

TypeScript Version: 3.9.2

Search Terms: #29571, "extends any"

Code

import ksm from "koa-session-minimal";

type KSMConfig = NonNullable<Parameters<typeof ksm>[0]>;
function initializeUserSession<T extends KSMConfig["store"]>(store: T): T {
    store.user = "test";
    return store;
}

Expected behavior:

store.user = "test" should succeed without failure.

Actual behavior:

Because KSMConfig["store"] is typed as any, due to the breaking change made in 3.9 to the behavior of extends any, the type gets reinterpreted as unknown and the compiler exclaims:

Property 'user' does not exist on type 'T'.(2339)

Playground Link: playground

Third-party typings for Javascript libraries often substitute any for incomplete types. This breaking change makes it hard to write functions that transform third-party types that reduce to any.

Using type assertions to fixup the type afterward is cumbersome.

    (store as KSMConfig["store"]).user = "test";

Could the change be relaxed to alias any to unknown only when any is explicitly requested? i.e.

function foo<T extends any>(x: T) {
    // x is unknown
}

type SomeType = any;
function bar<T extends SomeType>(x: T) {
    // x is SomeType (any)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions