Live data from Hacker News

HypeScript: Simplified TypeScript type system in TypeScript's own type system

github.com

81–84 of 84 posts

Re: HypeScript: Simplified TypeScript type system in TypeScript's own type system

#81

Earlier quoted context omitted.

What would you use for a field called "stage" that could be set to 'dev' or 'stage' or 'prod'? Or other enum-type things? I've seen pure JS enums like {'dev' : Symbol('dev'), 'prod' : etc} but I never use them because you can't send them over the wire. You could use a type like {dev:true} | {prod:true} | {stage:true} but you still wind up comparing strings.

> {dev:true} | {prod:true} | {stage:true} This can't be accessed safely. Because all value in typescript can be sub type of how it typed. It means code following will pass. var a = { dev: true, get prod() { throw Error() } } var b: { dev:true } | {prod:true} | {stage:true} = a And there is no guarantee that access any of these fields is safe.

That did not compile for me with my TS settings;

> Type '{ dev: boolean; readonly prod: void; }' is not assignable to type '{ prod: true; }'

It's still a bad solution though; it allows nonsense values like {dev:true, prod:true}.

Re: HypeScript: Simplified TypeScript type system in TypeScript's own type system

#82

Earlier quoted context omitted.

Interesting. > Instead, encourage programming patterns that do not require run-time metadata. I can understand why. But there are times when any alternative pattern is a worse choice. Then it's left to the coder to hack together type checks however it's convenient. Does the Dog class really need a static variable typed to Dog|Cat... ? It would be nice if instead of a dozen nonstandard ways to do that, some metadata c…

> Does the Dog class really need a static variable typed to Dog|Cat... ? It doesn't- because classes are a native JS concept and not a TS concept; they already have the metadata to distinguish them > TS does weird stuff like emitting functions to define enums, for instance Not sure what's meant by this; TypeScript does introduce enum syntax which technically generates runtime code (one of the few instances), though i…

> classes are a native JS concept

Recently, with ES6. But JS still lacks a way to get the type of an instance. You can check it but you can't simply get it, as in `new typeof(this)`. So you have to store it somewhere in the instance or in the type, as you would for a manual typeguard. And much of TS usage is still not for generating ES6 code. TS overlays boilerplate to mimick classes on ES5 targets. As it mimicks enums. I'm just saying I don't see why it couldn't mimick type reflection as well.

Re: HypeScript: Simplified TypeScript type system in TypeScript's own type system

#83

Earlier quoted context omitted.

> {dev:true} | {prod:true} | {stage:true} This can't be accessed safely. Because all value in typescript can be sub type of how it typed. It means code following will pass. var a = { dev: true, get prod() { throw Error() } } var b: { dev:true } | {prod:true} | {stage:true} = a And there is no guarantee that access any of these fields is safe.

That did not compile for me with my TS settings; > Type '{ dev: boolean; readonly prod: void; }' is not assignable to type '{ prod: true; }' It's still a bad solution though; it allows nonsense values like {dev:true, prod:true}.

That is because I forgot to assert the true as literal true, so it defaults to be boolean (where both true and false are allowed)

Re: HypeScript: Simplified TypeScript type system in TypeScript's own type system

#84
post #7

For people used to working in TypeScript who suddenly find themselves having to work on a vanilla js app... and don't wanna use JSDoc?

Do everything you can to introduce TS incrementally. JSDoc simply isn’t enough.
Post reply on HN