microsoft / microsoft/TypeScript

Empty type inferred by Object.entries

Open
#21,826 16 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: lib.d.ts
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 2.7.1

Search Terms: Object entries empty TS2365

Code

let o: any = {x: 5};

let p = Object.entries(o).map(([k, v]) => v + 1);

Compile command: tsc --lib es2017,es2017.object a.ts

Expected behavior:
This should compile without any error, assuming v: any.
This was the case for Typescript 2.6.2 at least.

Actual behavior:
(3,43): error TS2365: Operator '+' cannot be applied to types '{}' and '1'.

Following codes compile without errors:

let o: object = {x: 5};

let p = Object.entries(o).map(([k, v]) => v + 1);
let o: any = {x: 5};

let p = Object.entries(o).map(([k, v]: [any, any]) => v + 1);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report in a.ts with TypeScript 2.7.1 using tsc --lib es2017,es2017.object a.ts, then compare the Object.entries inference with the 2.6.2 behavior described here. Start by tracing the typings and inferred tuple element types for the shown map callback. Done when the original example compiles as expected without breaking the explicitly annotated alternatives.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.