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 to specify return type of constructor

See original GitHub issue

Search Terms

constructor return type

Suggestion

Add an ability to specify return type of a class constructor. Right now the compiler produces the following error: “Type annotation cannot appear on a constructor declaration.”

Use Cases

Consider this example:

interface MyInterface {
  readonly data: ReadonlyArray<string>

  add(item: string): void
  remove(item: string): void

  // ...more
}

class MyClass implements MyInterface {
  data = [] as string[]
  
  constructor(): MyInterface {} // right now it's an error

  add(item: string) {
    this.data.push(item)
  }

  // ...more
}

Current behavior:

const c = new MyClass() // typeof c == MyClass
c.data = [] // oops! no error from the compiler

Suggested behavior:

const c = new MyClass() // typeof c == MyInterface
c.data = [] // the compiler generates an error here, correct behavior

Of course, there are several ways to achieve this result (like using private constructor with factory function, or using a type converter like this: type AsInterface<T, C> = C extends new (...args: infer A) => T ? new (...args: A) => T : never But I think that the proposed feature is more concise

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. new expression-level syntax)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:33
  • Comments:28 (5 by maintainers)

github_iconTop GitHub Comments

22reactions
saintcrawlercommented, Oct 7, 2018

And what if I “forgot” to do that? This behavior should be enforced by creator of the class, not by consumer

13reactions
RReversercommented, Dec 18, 2019

Another usecase of this feature that didn’t seem to be mentioned above is specialisation. E.g. I want to be able to write this in TS defintions:

declare class C<R> {
    constructor(source: number): C<Uint8Array>;
    constructor(source: R);
    ...
}

and have it behave like normal function overloads.

For now the only way to achieve this is via var+interface declarations, which is less pleasant / obvious than a direct syntax would be:

interface C<R> {
    ...
}

declare const C: {
    new(source: number): C<Uint8Array>;
    new<R>(source: R): C<R>;
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Constructor Return Type in Java
Therefore, the return type of a constructor in Java and JVM is void. Taking another look at our simple assignment: Color color =...
Read more >
Why do constructors in java not have a return type?
Constructor is internally a nonstatic method with name <init> and void return type. It does not return anything. Internally first object is allocated...
Read more >
Constructors in Java
Constructors do not return any type while method(s) have the return type or void if does not return any value. Constructors are called...
Read more >
Compiler Error C2533
A constructor cannot have a return type (not even a void return type). A common source of this error is a missing semicolon...
Read more >
PHP RFC: Allow void return type on constructors/destructors
This RFC proposes to allow specifying void return type on constructors and destructors. In a way, this optional return type is like a ......
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