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

Evaluate compile-time constants #31352

Closed
n0izn0iz opened this issue May 11, 2019 · 10 comments
Closed

Evaluate compile-time constants #31352

n0izn0iz opened this issue May 11, 2019 · 10 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@n0izn0iz
Copy link

TypeScript Version: 3.5.0-dev.20190511

Search Terms:

compile-time operations numerical constants

Code

main.ts

const TWO: 2 = 1 + 1;

Expected behavior:

Transpiles without error

Actual behavior:

$> tsc main.ts

main.ts:1:7 - error TS2322: Type 'number' is not assignable to type '2'.

Playground Link:

https://www.typescriptlang.org/play/#src=const%20TWO%3A%202%20%3D%201%20%2B%201%3B

Related Issues:

@n0izn0iz
Copy link
Author

n0izn0iz commented May 11, 2019

Fun times:

const TWO: 2 = (1 + 3) as 2;
console.log(TWO);

Transpiles and executes, printing

4

EDIT: Please ignore this one

@MartinJohns
Copy link
Contributor

Transpiles and executes, printing....

Type-assertions are a way to tell the compiler: "Trust me on this, I know what I'm doing and what this type is". The compiler trusts you. In this case the compiler resolves the type number, and you tell the compiler to assume it is the literal type 2. You get the same result with const two: 2 = 4 as 2;.

@jcalz
Copy link
Contributor

jcalz commented May 12, 2019

Duplicate of #15645

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds labels May 13, 2019
@RyanCavanaugh
Copy link
Member

Without some motivating use cases this doesn't look compelling as an area of investment. The intersection of areas where you have a finite domain of numbers (literals) but want to do some computation to map to one of those members seems very small.

@n0izn0iz
Copy link
Author

const MESSAGE_BYTES = 32;
const BYTE_BITS = 8;
type SHA3Size = 224 | 256 | 384 | 512;
const SHA3_SIZE : SHA3Size = MESSAGE_BYTES * BYTE_BITS;
const hasher = SHA3(SHA3_SIZE)

I would understand if this not compelling enough.
Code is better than comments IMO. Also I guess my hatred for magic numbers is higher than the average.

@MartinJohns
Copy link
Contributor

@n0izn0iz That looks more like "Units of measure": #364

@RyanCavanaugh
Copy link
Member

8 * 32 seems like a much worse way to write 256, but I guess it's a matter of taste

@n0izn0iz
Copy link
Author

Even if MESSAGE_BYTES is a constant used in another part of the program?

@Morglod
Copy link

Morglod commented Apr 2, 2021

https://github.com/Morglod/tsts

Currently working on comptime transformer

const numbers = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
const result = comptime(() => {
    return numbers.reduce((total, x) => sum(total, x), 0);
});

to

const result = (() => { return (36); })();

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Too Complex" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

6 participants