microsoft / microsoft/TypeScript

Typescript can't infer types when using Proxy

Open
#20,846 12 comments 102 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code

let obj = {
    prop1: function () { },
    prop2: 'hello',
}

let prox = new Proxy(obj, {
    get: function (target, name) {
        return 5;
    }
});

prox.prop1.

Expected behavior:
I would expect that when I type prox.prop1., I would get typescript suggestions for Number.prototype, but instead, I get suggestions for Function.prototype. prox.prop1 will (according to typescript) still be callable as a function, but in runtime, it will clearly be a number and will throw an exception.

Statically evaluate the proxy traps and determine the type of thing being returned to offer proper typescript intellisense.

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

The issue supplies no repository file or test entry point. Reproduce the provided Proxy example in a TypeScript editor, compare completion for prox.prop1. with the runtime result, and treat correct Number.prototype suggestions instead of Function.prototype suggestions as done.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.