google / google/closure-compiler
typeof operator used on an inferred record type produces a cryptic error
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
As described [here](https://github.com/google/closure-compiler/wiki/Types-in-the-Closure-Type-System), the `typeof` operator is probably not intended to be used on inferred types. However, in Typescript it is possible to do it, and it is in fact a very common use case.
Here is an example of using the typeof operator on an inferred record type.
```js
const x = {
name: "Bat",
surname: "Man"
}
/**
* @type {typeof x} // This raises cryptic error
*/
const y = {
name: "Cat",
surname: "Woman"
}
```
And it produces this error:
```
./src/js/index.js:10:12: WARNING - [JSC_TYPE_MISMATCH] initializing variable
found : {name: string, surname: string}
required: {name: string, surname: string}
10| const y = {
^
11| name: "Cat",
^^^^^^^^^^^^^^^^
12| surname: "Woman"
^^^^^^^^^^^^^^^^^^^^
13| }
^^^
```
This error message does not make a lot of sense. Two identical types do not match? A better readable error message would be along the lines of "It is forbidden to use typeof operator on inferred types / inferred record types".
Compiler Version: v20221102
Build command:
```
java -jar ./scripts/closureCompiler.jar \
--entry_point=./src/js/index.js \
--js=./src/**.js \
--dependency_mode=PRUNE \
--warning_level=VERBOSE \
--js_output_file=./dist/bundle.js \
--module_resolution=WEBPACK \
--compilation_level=ADVANCED \
--jscomp_error=checkDebuggerStatement \
--jscomp_error=unusedLocalVariables \
--jscomp_error=reportUnknownTypes;
```
Contributor guide
Assessment
This issue has not been assessed yet.