Skip to content

Lib: args rest type should always be array of any #36327

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

Closed
antongolub opened this issue Jan 21, 2020 · 4 comments
Closed

Lib: args rest type should always be array of any #36327

antongolub opened this issue Jan 21, 2020 · 4 comments

Comments

@antongolub
Copy link

TypeScript Version: 3.8.0-dev.20200119

Search Terms: rest, ConstructorParameters, ReturnType
Code

/**
 * Obtain the parameters of a constructor function type in a tuple
 */
type ConstructorParameters<T extends new (...args: any) => any> = T extends new (...args: infer P) => any ? P : never;

Expected behavior:
...args rest param should extend any[]

Actual behavior:
...args has any type

Playground Link:

Related Issues:

@dragomirtitian
Copy link
Contributor

dragomirtitian commented Jan 22, 2020

Why is this an issue ? any is assignable to anything, including to any[] and can be the type of a rest parameter. Is this causing any problems anywhere? I can't think of any problems this would cause. I don't think changing this for the sake of change is worth it.

@antongolub
Copy link
Author

antongolub commented Jan 22, 2020

I experimented with mixin factories and got signature mismatch of my IConstructable type when I unfolded the inheritance chain and tried to check the type through InstanceType<typeof MixinResultClass>

type IConstructable<T= {}, A extends any[] = any[]> = new (...args: A) => T
<T extends new (...args: any) => any>

As I understand any[] extends any, but any does not extend any[]

@dragomirtitian
Copy link
Contributor

@antongolub Maybe post a self contained example reproducing the issue?

@antongolub
Copy link
Author

Well, once I tried to localize problem point, I've found accidental iface and variable names clash.

interface D {
}
// ~200 lines of code
const D = ... 

This made a major contribution to my problem.
So this PR improvement does not make any sense to my original goal: verify that custom IConstructables corresponds TS expectations. My hypothesis was that I could check new ()... method signature vs T extends new(args: any[]) condition. I was wrong. For example, new(...args: null) in some interface passes the check.

type InstanceType<T> = T extends new (...args: any[]) => infer R ? R : any;

interface Foo { foo: string }

interface WeirdConstructable {
  new (...args: never): Foo
}

const instance: InstanceType<WeirdConstructable> = { foo: 1 } // $ExpectError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants