Skip to content

Proposal: Comptime. Optimization hints for PreprocessorsΒ #49052

Closed as not planned
@lucsoft

Description

@lucsoft

Suggestion

πŸ” Search Terms

compiler preprocessor inline comptime

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • 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
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Add syntax hints that preprocessor can use.
Typescript should add this so developers would have a common interface to tell there toolchains (in this context: typescript aware preprocessors) which functions or expression can be evaluate at compile time.
Preprocessor could then evaluate the runtime code and make it run at compiletime and serve its response static.

This Suggestion is not the first idea proposed here, but it tries to fix the problem the other ones had.

🚫 Non Goals

Typescript should not evaluate the code itself. (Which #26534 suffered from)
Typescript should not throw any comptime errors

πŸ“ƒ Motivating Example

This Idea is nothing new. Zig is one of the biggest languages which have implemented this feature.

And comptime is a great feature for the web, as code size matters (like for loading speeds). Yet we want readable code, which often suffers from more code. more readable code.

https://kristoff.it/blog/what-is-zig-comptime/
https://ziglang.org/documentation/master/#comptime

πŸ’» Use Cases

1. comptime for setting a variable

New Valid Syntax:

const compiledAt = comptime new Date().getTime()
// or
const compiledAt = new Date().getTime() as comptime

Compiles directly to:

const compiledAt = new Date().getTime()

A Typescript aware preprocessor would turn this into:

const compiledAt = 1652206435966
2. comptime getting used in a function

New Valid Syntax:

comptime async function readConfigFile() {
      const data = await Deno.readTextFile("config.json");
      return JSON.parse(data);
}

console.log((await readConfigFile()).version)

Compiles directly to:

async function readConfigFile() {
      const data = await Deno.readTextFile("config.json");
      return JSON.parse(data);
}

console.log((await readConfigFile()).version)

A Typescript aware preprocessor would turn this into:

console.log("1.0.0-beta.1");
3. comptime validation depending on function paramets

New Valid Syntax:

// "comptime id" declares that this parameter needs to be known at compile time (aka preprocessor time)
function registerView(comptime id: string) {
      comptime: {
             if (!id.includes("-"))
                    throw new Error("Invalid ID");
             if (id.toUpperCase() != id)
                    throw new Error("Invalid ID");
      }
      // Normal code goes here [ ... ]
}

registerView("Hey-100") // Valid for Typescript
registerView("HEY-100") // Valid for Typescript

Compiles directly to:

function registerView(id) {
      comptime: {  // A "Label" is a valid javascript syntax
             if (!id.includes("-"))
                    throw new Error("Invalid ID");
             if (id.toUpperCase() != id)
                    throw new Error("Invalid ID");
      }
      // Normal code goes here [ ... ]
}

registerView("Hey-100") // Valid for Javascript, Throws a runtime error (which could lead to a unknown bug)
registerView("HEY-100") // Valid for Javascript

A Typescript aware preprocessor would turn this into:

function registerView(id: string) {
      // Normal code goes here [ ... ]
}

registerView("Hey-100") // Invalid for the Preprocessor, Throws a compiletime error (which would identify the bug directly)
registerView("HEY-100") // Valid for the Preprocessor

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions