You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// A *self-contained* demonstration of the problem follows...functionabc(a: number){if(a===1){return{a:1}}if(a===2){return{b:2}}return{c:3}}console.log(abc(3).c)
Expected behavior:
The compiler know that abc(3) has the property c. Actual behavior:
It doesn't.
My thoughts:
Since in abc(3), the argument is a known value, and abc doesn't rely on any outter closure, it can be calculated at compile time, the compiler should know the result has the property c.
And of course:
constarg1=3;console.log(abc(arg1).c);// it should workletarg2=3;console.log(abc(arg2).c);// it shouldn't workfunctionabc2(a: number){if(a===1&&window.whatever===true){return{a:1}}if(a===2){return{b:2}}return{c:3}}console.log(abc2(3).c);// it shouldn't work
Just like the title: Calculate everything can be calculated at compile time.
In fact, the above is not what I really want. What I really want is JUST TRY. Try to run every part of the code. If it can go on, then we know the type, we can even know the result value. And we can even just replace the expression with the result value for a better running performance and a smaller bundled size.
We do not need a complex type system. What we need is just running my code and leave out what you do not know now, then tell me what I can type at next character.
The text was updated successfully, but these errors were encountered:
@mhegazy I added something just now. If only you can view it once more.
And what I want is slightly different from #1151
In #1151 :
functionadd_six(a : number){returna+4+2;}// #1151 want it be translated tofunctionadd_six(a : number){returna+6;}// but I just want code like:add_six(3)// be translated to9// and of courseadd_six('3')// be translated to (if it can)'342'
If I am not wrong, you want the compiler to support constant propagation/folding. This is a common optimizations that compilers do. I do believe this is the same request as in #720 and #1151.
While this is a useful feature, I do not believe it fits within the scope of the TypeScript project at the time being.
The same argument in #1151 (comment) still holds i would say. There is no specific type system related information that is needed to do this analysis and can well be done on the generated JS code directly. My recommendation for these topics in general is to use a tool like Closure Compiler (ADVANCED_OPTIMIZATIONS mode does supports constant folding) or uglify JS.
TypeScript Version: 2.0.3 / nightly (2.1.0-dev.201xxxxx)
Code
Expected behavior:
The compiler know that
abc(3)
has the propertyc
.Actual behavior:
It doesn't.
My thoughts:
Since in
abc(3)
, the argument is a known value, andabc
doesn't rely on any outter closure, it can be calculated at compile time, the compiler should know the result has the propertyc
.And of course:
Just like the title: Calculate everything can be calculated at compile time.
In fact, the above is not what I really want. What I really want is JUST TRY. Try to run every part of the code. If it can go on, then we know the type, we can even know the result value. And we can even just replace the expression with the result value for a better running performance and a smaller bundled size.
We do not need a complex type system. What we need is just running my code and leave out what you do not know now, then tell me what I can type at next character.
The text was updated successfully, but these errors were encountered: