Microsoft/TypeScript

Type Inference in for... in combined with typeof

Open

#12.944 aperta il 15 dic 2016

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
Help WantedSuggestion

Metriche repository

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

Descrizione

TypeScript Version: 2.1.4

The following code gives an error in TS 2.1: It's not really nice code, but I think it should work never the less :)

for (var a in this) {
            if (typeof this[a] === "function" && !ko.isObservable(this[a]))
                this[a] = this[a].bind(this);
        }

Expected behavior: I would expect this[a] to be typed as this[keyof this] & Function:

for (var a in this) {
            if (typeof this[a] === "function" && !ko.isObservable(this[a]))
                this[a] =( <this[keyof this] & Function> this[a]).bind(this);
        }

Actual behavior: It gives an error, as of TS 2.1, it worked in TS 2.0 (because mapped types did not exist, this[a] was typed as any)

Guida contributor