Monday, September 7, 2020

typescript is great! in all seriousness, i pretty much would never want to do regular js ever again after using ts.

except for all the ways it isn't. kinda depressing how it comes across to me as being over advertised, over hyped, has lies by omission, leads people down garden paths into being painted into corners.

to wit: there is a lot of stuff online about how wonderful interfaces are in typescript, and i do like the idea of some of the pros about structural typing over nominal typing. but it turns out that if you choose to try to go with interfaces for the most part, you are not going to be able to do some things that you might at some point very desperately wish you could do, like instanceof since that only works with classes and their constructors. i mean sure it is ok for the two approaches to be different, but it would help if the actual "oh three weeks from now you are going to be kind of screwed and feel like you need to rewrite all your code" type gotchyas were more explicitly called out up front.

there's other things that are real sharp edges in the typescript system i suspect because they are (a) hampered by the hell that is javascript itself and (b) they have gone a little too bananas overboard with the cool things they could possibly do even though they apparently try to not do too many things. to wit see the problem i ran into with exhaustiveness checking of enums, code at the end of the post. (to be fair, enums are a broken living hell in almost every language, even the ones that aren't saddled with backwards compatibility.) also there's the problem where type aliases cannot be used everywhere a type can be used, which is really pretty horribly lame iuam.

...and then seems to have a rather broken discord system.

me writing to "Robert" who has set up the help cooldown and who says that if cooldown is broken to message him directly to fix it: "hi, apparently the help cooldown hasn't gone away for me although it[']s been like 20+ hours."

BOT replying to me: "Your message could not be delivered because you don't share a server with the recipient or you disabled direct messages on your shared server, recipient is only accepting direct messages from friends, or you were blocked by the recipient."

...so i am kind of dead in the water with tsc right now because of something that looks like this but worse where i cannot get the compiler to actually work correctly at all right now.

yay! so my 6667+ lines of code are now kinda like a dead albatross around my neck? would i have been much better off with Flow? can i easily strip all the typescript stuff from my ts code to get vanilla js? i think that was easy with flow back when i was uing it, but i dunno these days.





/* !!! update: the issue turned out to be that "_: never" is the wrong thing to write. the internet helped me find out (certainly the typescript compiler did not) that i should instead use "_: unknown" and then the hallowed exhaustiveness checking started working for me, yay !!! */

function unreachable(_: never): never {
throw new Error("unreachable");
}

const enum Facing {
left,
right,
}

function on_facing<R>(facing: Facing, left: R, right: R): R {
switch (facing) {
case Facing.left: return left;
case Facing.right: return right;
default: unreachable(facing);
}
}

const enum FacingMask {
left = 1 << 0,
right = 1 << 1,
}

function on_facingMask<R>(facing: FacingMask, left: R, right: R): R {
switch (facing) {
case FacingMask.left: return left;
case FacingMask.right: return right;
default: unreachable(facing);
}
}

No comments:

Post a Comment