Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
webstrand opened this issue May 13, 2020 · 1 comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@webstrand
Copy link
Contributor

webstrand commented May 13, 2020

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)
}
@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels May 14, 2020
@webstrand
Copy link
Contributor Author

I haven't had any further trouble with this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants