Allow to specify return type of constructor
See original GitHub issueSearch 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:
- Created 5 years ago
- Reactions:33
- Comments:28 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
And what if I “forgot” to do that? This behavior should be enforced by creator of the class, not by consumer
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:
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: