functionA(){this.p=0}A.prototype.m=function(x){this.p=x}vara=newA()a.p// p: anyExpected behavior:
a.p: number
Actual behavior:
a.p: any
This happens because we treat all assignments to the property as declarations and then union their types. And any | T | ... === any
But compare:
functionA(){this.ps=[]this.maybe=undefined}A.prototype.init=function(x){this.ps=[1,2,3]this.maybe='just'}vara=newA()a.ps// ps: number[]a.maybe// maybe: string | undefinedWhen the initialiser in the constructor has an uninformative type, the unioning can result in a much better type, or at least a less bad type (any). But perhaps the fix here is to handle these uninformative initialisers differently.
Expected behavior:
a.p: numberActual behavior:
a.p: anyThis happens because we treat all assignments to the property as declarations and then union their types. And
any | T | ... === anyBut compare:
When the initialiser in the constructor has an uninformative type, the unioning can result in a much better type, or at least a less bad type (any). But perhaps the fix here is to handle these uninformative initialisers differently.