Skip to content

Compiler calculated values support #720

Closed
@lee-elenbaas

Description

@lee-elenbaas

I would love to see support for compiler calculated constants:
Things like:

var msg = 'Hello'+' '+'world'+'!';
var ratio = 3/1;
var height = 30;
var width = height * ratio;

i like them to be translated to something like this:

var msg = 'Hello world!'; // 'Hello'+' '+'world'+'!'
var ratio = 3; // 3/1
var height = 30;
var width = 90; // height * ratio

This will allow writing a more readable code, without paying the computational price at run-time.

This idea can be expanded to also support pre-calculation of functional only code:

module x {
function fib(n) { // yes i know very naive implementation with no thinking about performance
  if (n <= 0) return 1;
  return fib(n-1)+fib(n-2);
}
export var v = fib(5);
}

can be translated to:

var x;
(function (x) {
    // omitted since it is unused outside its scope, and all its uses are pre-calculated
    // function fib(n) {
    //     if (n <= 0)
    //         return 1;
    //     return fib(n - 1) + fib(n - 2);
    // }
    x.v = 8; // fib(5)
})(x || (x = {}));

this will force the compiler to mark as part of its type information. whether something is functional or has side effects (accessing anything outside its internal scope), and where code is usable from, and whether or not it is ever used from within any function that already exists.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it adds

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions