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

'unknown' is not assignable to type for static API with generic methods #46662

Closed
marikaner opened this issue Nov 3, 2021 · 2 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@marikaner
Copy link

Bug Report

I am trying to have an interface (Constructable) for the static API of a class (SomeClass), which I want to be able to pass to some function (fn).
I have some static methods in the static API, that should allow to pass parameters with generic types.
I can't get the generic types to be assignable to one another.

🔎 Search Terms

'unknown' is not assignable to type

Possibly related to #45255

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about - couldn't find anything relevant

⏯ Playground Link

Playground link with relevant code

💻 Code

interface Constructable<T> {
    new (...args: any[]): T;
    myStaticMethod<P extends any>(param: P): void; // why is this treated as P extends unknown ?
}

class SomeClass {
    static myStaticMethod<P extends string>(param: P): void { }
}

function fn(ctor: Constructable<SomeClass>): void {}

fn(SomeClass);

🙁 Actual behavior

I cannot call fn with SomeClass because the generic type "'unknown' is not assignable to type string". This also does not work if I change the Constructable interface to have this method: myStaticMethod<P>(param: P): void; (generic parameter does not extend any). Then the error is simply "'P' is not assignable to type string".

🙂 Expected behavior

I expected that SomeClass is assignable to the type Constructable<SomeClass>.

@MartinJohns
Copy link
Contributor

MartinJohns commented Nov 3, 2021

// why is this treated as P extends unknown ?

See the release notes for TypeScript 3.9: Type Parameters That Extend any No Longer Act as any, and also #29571 / #29509.

@andrewbranch andrewbranch added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 3, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants