Skip to content

Suggestion: Constructor<T> and Class<T> #20256

Closed
@hatzka-nezumi

Description

@hatzka-nezumi
type Constructor<T> = new(...x: any[]) => T;

type Class<T> = Constructor<T> & {
    prototype: T;
}

The use case for these should be fairly obvious. I'm separating them because, according to MDN, a constructor could choose to return an object that doesn't actually have the constructor's .prototype as its prototype -- but this doesn't actually work in Chrome for me; is that a bug in Chrome or an inaccuracy in MDN? I can't follow the spec well enough to tell either way.

It's worth noting that Constructor<T> should be revised if #5453 or #12265 get added, to the effect of

type Constructor<T, ...U> = {
    new(...U): T;
}

type Class<T, ...U> = Constructor<T, ...U> & {
    prototype: T;
}

but backwards compatibility there requires type argument defaults (I swear I've seen a proposal for that, but I can't find it, or I'd link it too); maybe it would be better to prefix these versions with Vague or something to that effect?

Activity

mhegazy

mhegazy commented on Nov 27, 2017

@mhegazy
Contributor

We limit the number of additional declarations added to the library to avoid namespace pollution, and to limit back-compat liabilities. You are welcome to define these type aliases as noted above in your own project and leverage them as needed.

added
DeclinedThe issue was declined as something which matches the TypeScript vision
on Nov 27, 2017
typescript-bot

typescript-bot commented on Dec 12, 2017

@typescript-bot
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript vision

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mhegazy@hatzka-nezumi@typescript-bot

        Issue actions

          Suggestion: Constructor<T> and Class<T> · Issue #20256 · microsoft/TypeScript