google / google/closure-compiler

Type inference with template in nested type not recognized

Open
#600 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

Hi,

the following code should not produce any errors:

``` javascript
/**
* @constructor
*/
var a = function() {};

/**
* @constructor
* @param {!T|function(new:T):undefined} x
* ^ 'x' is set to receive an instance or the class itself of a type
* @template T
*/
var b = function(x) {
this.x = x;
};

/** @type {!b.} */
var c = new b(a); //Right
/** @type {!b.} */
var d = new b(new a()); //Right
```

but it produces:

```
test.js:17: ERROR - initializing variable
found : b.<(a|function (new:a): undefined)>
required: b.
var c = new b(a); //Right
^

1 error(s), 0 warning(s), 91.7% typed
```

`b` has a template type, which at the constructor can be received as the class or as an instance of that class. The inference type system believes `T` is any of the possible types of the parameter (`!T|function(new:T):undefined`), instead of the type in the placeholder `T`. It effectively matches to the left, because the whole type matches the parameter, but doesn't match the right side, because instead of inferring the constructor type, it belives `T` is the constructor itself.

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.