Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUGGESTIONS]Calculate everything can be calculated at compile time. #12104

Closed
xialvjun opened this issue Nov 8, 2016 · 3 comments
Closed

[SUGGESTIONS]Calculate everything can be calculated at compile time. #12104

xialvjun opened this issue Nov 8, 2016 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@xialvjun
Copy link

xialvjun commented Nov 8, 2016

TypeScript Version: 2.0.3 / nightly (2.1.0-dev.201xxxxx)

Code

// A *self-contained* demonstration of the problem follows...
function abc(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:

const arg1 = 3;
console.log(abc(arg1).c);  // it should work

let arg2 = 3;
console.log(abc(arg2).c);  // it shouldn't work

function abc2(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.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 8, 2016

Looks like a duplicate of #720 and #1151.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 8, 2016
@xialvjun
Copy link
Author

xialvjun commented Nov 8, 2016

@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 :

function add_six(a : number) {
    return a + 4 + 2;
}

// #1151 want it be translated to
function add_six(a : number) {
    return a + 6;
}

// but I just want code like:
add_six(3)
// be translated to
9
// and of course
add_six('3')
// be translated to  (if it can)
'342'

@mhegazy
Copy link
Contributor

mhegazy commented Nov 8, 2016

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.

@mhegazy mhegazy closed this as completed Apr 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants