This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.
AI solution for TypeScript puzzle on BFE.dev
11. implement ConstructorParameters<T>
Here is one possible implementation of MyConstructorParameters<T>
:
type MyConstructorParameters<T extends new (...args: any) => any> = T extends new (...args: infer P) => any ? P : never;
Explanation:
T extends new (...args: any) => any
ensures thatT
is a constructor function....args: infer P
captures the types of the constructor arguments into a tupleP
.T extends new (...args: infer P) => any ? P : never
uses a conditional type to returnP
ifT
extends the expected constructor signature, ornever
otherwise.
With this implementation, the C
type in the example will be correctly inferred as [a: string, b: number, c: boolean]
.