Typescript: Constant / compile-time-only functions

Created on 18 Aug 2018  路  4Comments  路  Source: microsoft/TypeScript

Search Terms

const compiler keyword functions

Suggestion

Allow for functions to be marked as 'const', a function marked as 'const' can be evaluated at compile time.

Allow for functions to be marked as 'compiler', a function marked with compiler can only be evaluated at compile time and is not emitted in the final code.

Functions marked with 'compiler' van only be called with arguments that are constants, or the result
from other 'compiler' functions.

Use Cases

Calculate and generate 'stuff' at compile time instead of runtime.

Examples

const x = concat("a","b");

const function concat(a: string, b: string) : string
{
    return a + b;
}

Compiles to (ES5):

var x = "ab";
function concat(a, b) {
    return a + b;
}

const x = concat("a","b");

compiler function concat(a: string, b: string) : string
{
    return a + b;
}

Compiles to (ES5):

var x = "ab";

const templateFunction = compileHandleBars("<p>{{a}}<p>");

compiler function compileHandleBars(template: string) : Function
{
    // code for compiling handlebars template ommitted.
}

Compiles to:

var templateFunction = function(object) {
    // generated handlebars code ommited.
}

const myVueTemplate = optimizeHtml(`
<div>
    <p>{{a}}</p>
</div>
`);

compiler function optimizeHtml(html: string) : string
{
    // code for optimizing html ommitted.
}

Compiles to:

var myVueTemplate = "<div><p>{{a}}</p></div>";

compiler const PngDataUriPrefix = "data:image/png;base64,";
const myImage = PngDataUriPrefix + getBinaryFileAsBase64("~/logo.png");

compiler function getBinaryFileAsBase64(path: string) : string
{
    // code for reading file from disk and base64-encoding ommitted
}

Compiles to:

var myImage = "data:image/png;base64,0lGODl ... ommitted... hEAAQAM=";

Checklist

My suggestion meets these guidelines:

  • [x] This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [ ] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. new expression-level syntax)
Out of Scope Suggestion

Most helpful comment

We don't ever do type directed emit, nor do we want to add additional expression-altering syntax (const enum was a big enough mistake already)

All 4 comments

This could be implemented without emitting different JS based on the types of the expressions

This proposal does depend on the type of function (compiler function or normal function), no?

I stand corrected. Proposal updated.

We don't ever do type directed emit, nor do we want to add additional expression-altering syntax (const enum was a big enough mistake already)

The main reason for this proposal was that all other build chain tools like Uglify, WebPack etc. are not TypeScript-aware. They use the output from the TS-compiler and generate less efficient and optimized code than would be possible if they were TypeScript aware.

Looks like I have to build or find a pre-compiler...

Off topic: I use const enum a lot, the generated code is a lot better than the code with a regular enum. If you could somehow make a regular enum behave like a const enum, if typescript detects it's used like a const enum, everyone would be happy.

Was this page helpful?
0 / 5 - 0 ratings