Microsoft/TypeScript

Typescript can't infer types when using Proxy

Open

#20.846 aperta il 21 dic 2017

Vedi su GitHub
 (12 commenti) (102 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
BugDomain: lib.d.tsHelp Wanted

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

Descrizione

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.

Guida contributor