google / google/closure-compiler

Instantiating templated function type has no effect

Open
#4,070 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

[Reproduction](https://closure-compiler-debugger.appspot.com/#input0%3D%252F**%250A%2520*%2520%2540template%2520T%250A%2520*%2520%2540param%2520%257BT%257D%2520input%250A%2520*%2520%2540return%2520%257BT%257D%250A%2520*%252F%250Afunction%2520identity(input)%2520%257Breturn%2520input%257D%253B%250A%250A%252F**%2520%2540typedef%2520%257Btypeof%2520identity%257D%2520*%252F%250Avar%2520Identity%253B%250A%250Avar%2520%252F**%2520Identity%253Cnumber%253E%2520*%252F%2520f%253B%250A%250Aconst%2520%252F**%2520null%2520*%252F%2520_%2520%253D%2520f%253B%250A%26input1%26conformanceConfig%26externs%26refasterjs-template%26CHECK_TYPES%3Dtrue%26REWRITE_MODULES_BEFORE_TYPECHECKING%3Dtrue%26CLOSURE_PASS%3Dtrue%26PARSE_TYPE_ANNOTATIONS%3Dtrue%26PRETTY_PRINT%3Dtrue):

```JS
/**
* @template T
* @param {T} input
* @return {T}
*/
function identity(input) {return input};

/** @typedef {typeof identity} */
var Identity;

var /** Identity */ f;

const /** null */ _ = f; // JSC_TYPE_MISMATCH: found: function(T): T
```

As far as I understand, the type of `f` should be `function(number): number` here.

See also [discussion in closure-compiler-discuss@](https://groups.google.com/g/closure-compiler-discuss/c/3jvNkjB7jS0).

Aside: I thought there might be a better way to define the `Identity` type, but I can't see in the documentation any way to declare a templated `function` type directly. (But I would expect to be able successfully instantiate a templated type regardless of how it was declared.)

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.