-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Math with Number Literal Type #26382
Comments
In #26223 I wondered aloud if there was a canonical issue for dependent types for tuples. Still not sure, but this issue is obviously related to that one. |
Duplicate of #15645 |
@AnyhowStep I'd been interested in this as well, be it also mostly for tuple manipulation -- given tuple indices are restricted to natural numbers, I don't care as much about math on fractions. I do see the TS team's considerations as valid though -- following a proposal like this would definitely add some complexity for the compiler. So features should be balanced with this complexity. As a compromise, I actually quite like the approach of Flow's utility types, offering functionality without actually adding new syntax. Now, your proposal to use these infix operators ( As much as I like it though, I think what sways considerations here toward compiler simplicity over low learning curve is, the target audience for type-level programming is very limited. We definitely need more features, but even if they're still hard to use, that won't affect most front-end engineers -- we'll type the harder bits for them in the big libraries, at which point they can enjoy the superior type checks without knowing what changed under the hood. I particularly like the (As a disclaimer, don't expect to just compile my collection though. Recursion proved fragile, while TS was a moving target and testing types across versions remained a challenge. In particular, a breaking recursive type meant non-terminating compilation, which was quite hard to catch in unit tests.)
@jcalz given
|
Given the recent changes with the type system (mostly lifting conditional recursion and the addition of template string types), has there been any further consideration of this issue? Though one of the concerns with this proposal was the possible union type explosion, such also applies to the recently added template string types, so it appears to not be an obstacle to this feature being implemented. Additionally, regarding the concern of the infix operators adding compiler complexity, the alternative of utility types can now be implemented, due to the addition of the |
Some additional thoughts on this proposal:
One argument for not including some of all of these features is that they may increase type checking time when used, but people who need them will implement these or similar on their own, which will be much less efficient than a few extra utility types that many of which simply map to JS built-ins under the hood. |
It's very helpful to me!
Cannot agree more. |
Definitely see this is needed. Agree with the points made by @tjjfvi . As TypeScript gain more and more attraction and becoming more mature, |
When I made this issue long ago, my only concern was math for integers and I think bigint wasn't really a thing yet. And I believe there was resistance to having type level math because of floating point wonkiness. So, the proposal was trying to avoid that wonkiness. Now that I look at it again, the wonkiness isn't so bad and if we need integer math, the bigint counterpart should be implemented. And maybe helpers to convert between bigint and number and string types |
Support for natural numbers and corresponding operations would be extremely useful! Plenty of examples of how this can let you do some really neat stuff can be found at https://github.com/granule-project/granule. |
I agree it would be cool and useful, but can imagine it doesn't have top priority. In the meantime, I've managed to get natural number calculation working up to 9999 in You can see it in action in this CodeSandbox |
Bumping this further with #48198 |
One example I'm facing today might be helped with this feature: const units = ["byte", "kilobyte", "megabyte", "gigabyte", "terabyte", "petabyte"] as const;
const i: number = /* some math to select an index */;
// Current behavior (typescript 4.7.3)
const last: string = units[i] ?? units[units.length - 1]; // ERROR: Type 'string | undefined' is not assignable to type 'string'. VSCode tells me that TypeScript sees // Expected behavior
const last: string = units[i] ?? units[units.length - 1]; // OK: Type 'typeof units[number] | "petabyte"' is definitely assignable to type 'string' I can work around this using the |
I definitely think this would be a good addition. If for no other reason than the fact that you can already do this in some really questionable ways using tuples and recursion. |
I also want this. I want to validate hex strings, which I could do if there were an easy way to do (integer) division in typescript's types. |
I think I made something quite close, it can type check numbers fast, up to 2**16 (=65_536), but the compiler will sometimes yell at you, especially when both the left-hand and right-hand sides are generic type X = Add<32_000, 32_123> // 64_123
type Y = Subtract<X, 64_000> // 123
type P = IsGreater<1_001, 1_000> // true const x: 64_123 = add(32_000, 32_123)
const y: 123 = subtract(x, 64_000)
const p: true = greater(1_001, 1_000) |
Can I point out that there's a case in the standard library where this feature is needed. Array's flat method takes a depth parameter which requires the ability to decrement a number literal in order to calculate the output type. Currently a tuple type is used with each element equal to one less than the index. I think it has 20 - 30 elements so a depth of, say, 40 would break it. Not terribly realistic I know, but theoretically a problem at least. |
A potential use case of this feature is to create tensors with determined size in deep learning libraries. Currently most deep learning framework are written in dynamically typed languages such as python, and often we'll find a misalignment of tensor shape halfway through the running. With this feature it'll be possible to write definitely-sized tensor in TypeScript. |
Just to share again, https://github.com/unional/type-plus has support some math functions. And it supports int/float/bigint/negative |
Are the performances great with large numbers and generics? |
I don't know about machine learning, but can you use this trick?
to create tensors with determined size e.g. |
I don't think there are any significant performance issue. The calculations are finite and not consuming unbound memory. |
Thanks for your great contribution on type calculation! I pretty appreciate the idea of transforming number literals into digit arrays and calculate. It's the most efficient way for implementing multiplication I have seen. However, it seems lacking support of division calculator (maybe precision issues concern?). Furthermore, I think it more elegant implementing these basical arithmetics with a language level support, rather than resorting to type gymnastics.
Certainly yes. But quite a number of tensor functions require support of some arithmetical calculations in its signature. A simple example is |
Definitely agree.
Yes, precision is the main concern. The other one is complexity. 😛 |
It doesn't have to support |
Just having intrinsic types for arithmetic operations could potentially allow improved performance on a variety of usecases. Although i believe we shouldn't also have intrinsics to parse strings, here is an example of RGB and HEX validation, as @AntonPieper suggested. It's a great example of a common usecase if you ask me. import { Call, N } from 'hotscript';
import { IsNumericLiteral } from 'type-fest';
// RGB color validation
type Int8<T extends number> =
IsNumericLiteral<T> extends true
? Call<N.GreaterThanOrEqual<T, 0>> extends true
? Call<N.LessThanOrEqual<T, 255>> extends true
? true
: false
: false
: false;
type CharToDigit = {
'0': 0;
'1': 1;
'2': 2;
'3': 3;
'4': 4;
'5': 5;
'6': 6;
'7': 7;
'8': 8;
'9': 9;
};
type Whitespace = ' ' | '\t' | '\n' | '\r';
// no negative, whitespace inbetween, decimal point or +3 digits
// removes whitespace at begin/end and reverses the string
type CleanAndReverse<
S extends string,
Acc extends string = '',
Count extends number = 0,
> = S extends `${infer First}${infer Rest}`
? First extends Digit
? Count extends 3
? never // too many digits
: CleanAndReverse<Rest, `${First}${Acc}`, Call<N.Add<Count, 1>>>
: First extends Whitespace
? Count extends 0 | 3 // whitespace only after 0 or 3 digits
? CleanAndReverse<Rest, Acc, Count>
: never // whitespace in wrong position
: never // invalid char (negative sign etc.)
: Acc;
type ParseReversedDecimalStr<
S extends string,
Multiplier extends number = 1,
Acc extends number = 0,
> = S extends `${infer Char}${infer Rest}`
? Char extends keyof CharToDigit
? ParseReversedDecimalStr<
Rest,
Call<N.Mul<Multiplier, 10>>,
Call<N.Add<Acc, Call<N.Mul<Multiplier, CharToDigit[Char]>>>>
>
: never
: Acc;
// provide reversed string to peano number parser
type ParseInt<S extends string> =
CleanAndReverse<S> extends ''
? never // empty values are not allowed
: ParseReversedDecimalStr<CleanAndReverse<S>>;
export type ValidateRGB<T> = T extends `rgb(${infer R},${infer G},${infer B})`
? Int8<ParseInt<R>> extends true
? Int8<ParseInt<G>> extends true
? Int8<ParseInt<B>> extends true
? T
: never
: never
: never
: never;
// Hex color validation
// eslint-disable-next-line prettier/prettier
type HexChar = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f';
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type Hex = (HexChar & string) | (Digit & string);
type StripHash<T> = T extends `#${infer S}` ? S : never;
type ParseHex<
T,
Acc extends string = '#',
Index extends number = 0,
> = T extends `${infer Char}${infer Rest}`
? Char extends Hex
? Rest extends ''
? Index extends 2 | 5 | 7
? `${Acc}${Char}` // final char
: never // wrong length
: ParseHex<Rest, `${Acc}${Char}`, Call<N.Add<Index, 1>>>
: never // wrong char
: never;
export type ValidateHex<T extends string> = ParseHex<StripHash<T>>;
// returns T as literal type if valid, otherwise never
export type ValidateColor<T extends string> = ValidateHex<T> | ValidateRGB<T>;
|
Working around microsoft/TypeScript#26382, which will likely never get solved.
Working around microsoft/TypeScript#26382, which will likely never get solved.
Working around microsoft/TypeScript#26382, which will likely never get solved.
Search Terms
number literal type, math
If there's already another such proposal, I apologize; I couldn't find it and have been asking on Gitter if such a proposal was already brought up every now and then.
Suggestion
We can have number literals as types,
If possible, math should be enabled with these number literals,
And comparisons,
And ways to convert between string literals and number literals,
Use Cases
One such use case (probably the most convincing one?) is using it to implement tuple operations.
Below, you'll find the types
Add<>, Subtract<>, NumberToString<>, StringToNumber<>
.They have been implemented with... Copy-pasting code until the desired length.
Then, using those four types, the tuple operations are implemented.
While this works, having to copy-paste isn't ideal and shows there's something lacking in the language.
I've found that I've had to increase the number of copy-pastes every few days/weeks as I realize I'm working with larger and larger tuples over time.
The below implementation also ignores negative numbers for simplicity but supporting negative numbers would be good.
Examples
Addition and subtraction should only allow integers
If we did allow
5.1 - 3.2
as a type, we would get1.8999999999999995
as a type.Maybe throw a compiler error on overflow with concrete numeric types substituted in,
Comparisons should work kind of like
extends
If either operand is
number
, the result should distributeDon't think floating-point comparison should be allowed.
Possible to have too many decimal places to represent accurately.
Converting between string and number literals is mostly for working with tuple indices,
Converting from integer string literals to number literals should be allowed, as long as within
MIN_SAFE_INTEGER
andMAX_SAFE_INTEGER
,but floating point should not be allowed, as it's possible that the string can be a floating point number that cannot be accurately represented.
For the same reason, converting floating point number literals to string literals shouldn't be allowed.
Checklist
My suggestion meets these guidelines:
Since this suggestion is purely about the type system, it shouldn't change any run-time behaviour, or cause any JS code to be emitted.
I'm pretty sure I've overlooked a million things in this proposal...
The text was updated successfully, but these errors were encountered: