google / google/closure-compiler

Rest operator used in object destructuring is not correctly type-checked

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

```js
/**
* @type {{ x: number, y: number, w: number, h: number }}
*/
const rect = { x: 0, y: 0, w: 100, h: 100 };

/**
* @typedef {{ x: number, y: number }}
*/
var Point;

const { w, h, ...point } = rect
/**
* @type {Point}
*/
const point2 = point
```

This produces the following error:
```
./src/js/index.js:22:15: WARNING - [JSC_TYPE_MISMATCH] initializing variable
found : Object
required: {
x: number,
y: number
}
missing : [x,y]
mismatch: []
22| const point2 = point
^^^^^
```
In essence, `point` variable is interpreted as `Object` type even though it clearly should have `{ x: number, y: number}` type.

Vaguely related to #3132

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 \
--jscomp_error=strictCheckTypes;
```

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.