google / google/closure-compiler

Cannot specify return type for class constructor

Open
#3,131 1 comment 0 reactions 1 assignee Claimed by @shicks View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

The compiler complains if the return type of a class constructor is not strictly an instance of this constructor. (Since I upgrade from the august version to v20181008).
As javascript allows to return anything from a constructor, the compiler should allow users to specify a custom return type using the "@return" doc.

Just for example if I do:
```
class Translator
{
/**
* Constructor with custom return type.
*
* @return {Promise.}
*/
constructor()
{
return this.bootstrap();
}

/**
* @return {Promise.}
*/
async bootstrap()
{
return this;
}
}
```

I don't want to get this warning:
```
Translator.js:10: WARNING - inconsistent return type
found : Promise
required: Translator
return this.bootstrap();
^^^^^^^^^^^^^^^^
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.