Typescript: numeric literal types disrespect arithmetics

Created on 8 May 2017  路  5Comments  路  Source: microsoft/TypeScript

declare var a : -1 | 0 | 1;
a = -a; // <-- problem
Awaiting More Feedback Suggestion

Most helpful comment

26382 has been determined to be a duplicate of this, although that one is about allowing arithmetic at the type level at all, while this one seems to be more concerned with inference and flow.

Anyway, I think that now that tuple types are becoming more powerful it might be reasonable to revisit this idea so that we can get closer to the sort of dependent types needed for strongly typing such methods as Function['bind']. (Related to #26223)

All 5 comments

It was one of the design decisions not do do any type arithmetic. see https://github.com/Microsoft/TypeScript/issues/8112.

Hm. Is that what #8112 meant by this?

Currently has some strange behavior for flow of arithmetic on types.

I hadn't managed to find mention of this in #7480 either. Were there any specific issues there we could think along on?

@mhegazy, out of curiosity, which problems did you find for flow of arithmetic on types? As @tycho01 pointed out, there is no reference to this in the issue you mentioned.

26382 has been determined to be a duplicate of this, although that one is about allowing arithmetic at the type level at all, while this one seems to be more concerned with inference and flow.

Anyway, I think that now that tuple types are becoming more powerful it might be reasonable to revisit this idea so that we can get closer to the sort of dependent types needed for strongly typing such methods as Function['bind']. (Related to #26223)

There are Inc<> and Dec<> (typical) and Minus<> (ts-toolbelt).

It is critical in doing type manipulation.
The custom implementation has limitation and not efficient at all.

With intrinsic in TypeScript 4.1, it would be great if TypeScript can provide those base types.

Was this page helpful?
0 / 5 - 0 ratings