microsoft / microsoft/TypeScript

ConstructorParameters is not working for private constructors (and protected constructors)

Open
#30,991 12 comments 44 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.4
Search Terms: ConstructorParameters, private constructor

Code

class Foo
{ public constructor(a: number, b: boolean, c: string) { } }

class Bar
{ private constructor(a: number, b: boolean, c: string) { } }

type FooCtorArgs = ConstructorParameters<typeof Foo>;
type BarCtorArgs = ConstructorParameters<typeof Bar>;

Expected behavior:
BarCtorArgs is [number, boolean, string].

Actual behavior:
Error in line 8:
Type 'typeof Bar' does not satisfy the constraint 'new (...args: any) => any'. Cannot assign a 'private' constructor type to a 'public' constructor type.

Playground Link: https://www.typescriptlang.org/play/#src=class%20Foo%0D%0A%7B%20public%20constructor(a%3A%20number%2C%20b%3A%20boolean%2C%20c%3A%20string)%20%7B%20%7D%20%7D%0D%0A%0D%0Aclass%20Bar%0D%0A%7B%20private%20constructor(a%3A%20number%2C%20b%3A%20boolean%2C%20c%3A%20string)%20%7B%20%7D%20%7D%0D%0A%0D%0Atype%20FooCtorArgs%20%3D%20ConstructorParameters%3Ctypeof%20Foo%3E%3B%0D%0Atype%20BarCtorArgs%20%3D%20ConstructorParameters%3Ctypeof%20Bar%3E%3B


Use Case

Convenient factory pattern to mimic asynchronous object construction.

class Component
{
    public static async create(...args: ConstructorParameters<typeof Component>)
    {
        const component = new Component(...args);
        await component.initialize();
        return component;
    }

    private constructor(a: number, b: boolean, c: string)
    { /* stuff */ }

    private async initialize()
    { /* asynchronous setup */ }
}

Ideas for Solution

Allow visibility modifiers in interfaces. Yes, if you use an interface to outline the visible properties of an object to a user this makes no sense, but why not allow interfaces to be a flexible tool to specify internal rules of implementations? I think this problem could be solved, if the ConstructorParameters type could be defined like the following:

type ConstructorParameters<T extends {private new (...args: any[]): any}> = T extends new (...args: infer P) => any ? P : never;

Which is of course no allowed at the moment.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the linked Playground reproduction and the TypeScript 3.4 behavior for ConstructorParameters applied to private and protected constructors. Identify the relevant compiler and regression-test entry points, then establish behavior that infers the constructor argument tuple without weakening visibility checks; done means the example type-checks with the expected tuple and coverage prevents regressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.