> In addition to generic interfaces, we can also create generic classes. Note that it is not possible to create generic enums and namespaces.
So... arbitrary opinionated restrictions in #TypeScript? Why not generic enums, actually?
> In addition to generic interfaces, we can also create generic classes. Note that it is not possible to create generic enums and namespaces.
So... arbitrary opinionated restrictions in #TypeScript? Why not generic enums, actually?
Let's say you have enum E<T> {...}. You have a function like this:
function dispatch<T>(e:E<T>): void {
switch (e) {
...
}
}
What goes in the cases?
I still think that generic enums would be useful for numeric computations as a type casting shortcut
enum Hello {
zero = 0.0,
one = 1.0,
e = 2.7,
pi = 3.14,
}
function whatever<T> (x: Hello<T>) {
switch(x) {
case Hello.zero:
return x + 8;
case Hello.one:
return x * 8;
case Hello.e:
return Math.pow(x, 3);
case Hello.e:
return Math.pow(x, 2) * 10;
}
}
Writing this, I realize that type casting doesn't really work in TS, with this (totally meaningful expression) still resulting in a float:
console.log((3.2 as unknown) as bigint);
So enum casting is actually not useful at all, and thus generic enums don't make sense. But that's not a problem of generic enums per se. It's a more general problem of TypeScript being both super restrictive and surprisingly fuzzy about types, to the point of absurd.
This is a small personal instance of Bonfire in the Fediverse.