question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow using type parameters and return types with overloaded class constructors

See original GitHub issue

Search Terms

Generic Class Constructor Declaration Overloads Type Parameters

Suggestion

This is partially a request to revisit some of the discussion from here: https://github.com/microsoft/TypeScript/issues/10860 But it’s also to provide some examples of cases that are difficult/impossible to type using classes today.

A class may frequently assign different types to its properties depending on how it was instantiated. While we can type those properties as unions of all their possible types (or even use completely separate classes), it would be amazing if we could “narrow” a class’s type based on how it was instantiated.

We CAN handle this type of complexity with regular function overloads, because the relevant call signature is narrowed by both function parameters and type parameters. Class constructors, however, only pay attention to the parameters being passed and can’t have type parameters.

Use Cases

// Using type parameters with overloaded generic function
function func(): void;
function func<T extends string>(requiredThing: T): void
function func(requiredThing?: string) { }
func()              // Ok
func<'hello'>()     // Expects 1 argument, as expected

// Attempting to use type parameters with overloaded class constructor
class Example<T> {
    constructor()
    constructor(blah: T)    // How do we connect this to the presence of T?
    constructor(blah?: T) {}
}
new Example()           // Ok
new Example<'hello'>()  // We want this to expect 1 argument somehow

As was also discussed in https://github.com/microsoft/TypeScript/issues/10860, return types on a constructor could theoretically represent narrowed versions of the instance type. Currently there’s not an easy way to narrow a property’s type based on how the class was instantiated:

class Example {
    prop?: string | number

    constructor()    // When this is used, prop should be string | number | undefined
    constructor(prop: string)    // When this is used, prop should be string
    constructor(prop: number) // When this is used, prop should be number
    constructor(prop?: string | number) {
        // implementation
    }
}

Examples

Ideally, maybe something like this for narrowing by type parameters:

class Example {
    constructor()
    constructor<T extends string>(blah: T)
    constructor(blah?: string) {}
}
new Example()           // Ok
new Example<'hello'>()  // Would expect 1 argument

And maybe something like this for return types:

interface IExampleString extends Example {
    prop: string
}
interface IExampleNumber extends Example {
    prop: number
}
class Example {
    prop?: string | number

    constructor()    // When this is used, prop should be string | number | undefined
    constructor(prop: string): IExampleString    // When this is used, prop should be string
    constructor(prop: number): IExampleNumber    // When this is used, prop should be number
    constructor(prop?: string | number) {
        // implementation
    }
}

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
fatcerberuscommented, Nov 27, 2019

do note that most of the built-in constructors are declared with type definitions like this and nobody seems to care much

I don’t care when writing code, that’s true, but I do start to care when I do a Peek Definition on types like these to, e.g., see available methods. It often takes me to the wrong half of the definition and the two halves don’t always seem to be kept together. Promise being a good example of that.

1reaction
rdhelmscommented, Dec 12, 2019

Also, I think it would be great to add an example somewhere to the documentation that displays this “separate instance type and constructor type” pattern that we’ve been talking about. I’ve found this setup below to be pretty powerful (in ambient contexts, anyway), but it took some mental hoop jumping to understand the idea of having an interface with the same name as a const, and the opportunity that it provides:

    interface SomeClassName {
        // All instance props and methods go here
    }
    const SomeClassName: {
        // All static props and methods AND overloaded construct signatures go here
        new(): SomeClassName
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Constructor Overloading in Java - GeeksforGeeks
This can be done using constructor overloading. For example, Thread class has 8 types of constructors. If we do not want to specify...
Read more >
Removing repetitive code (overloading methods and ...
Methods can be overloaded in the same way as constructors, i.e., multiple versions of a given method can be created. Once again, the...
Read more >
Documentation - Classes - TypeScript
Constructors can't have type parameters - these belong on the outer class declaration, which we'll learn about later; Constructors can't have return type...
Read more >
Constructor overload in TypeScript - Stack Overflow
Using a destructured parameter only allows methods and properties you've assigned to the object. For example, consider this method: class BoxTest { public ......
Read more >
Constructors overloading in c# - Complete C# Tutorial
It allows us to use a class in a different manner. The same class may behave different type based on constructors overloading. With...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found