Hacker News new | ask | show | jobs
by alcuadrado 1187 days ago
You can use typescript as jsdoc annotations
1 comments

Can you please expand further? If I annotated my js, how would the browser check and enforce my types? Would the checks include js-to-js code? What is the js script that I should include at the top of my <head>?
> how would the browser check and enforce my types?

If this is what you're looking for, you aren't going to get it with TypeScript, even if you could parse it in the browser. It might be possible to port or build a TypeScript parser that runs in the browser (it might even be done already) but this would mean sending a LOT of code over the wire for something that was designed as a static analysis tool.

You can use runtime validation libraries like https://zod.dev/.

JS will eventually include its own type system so that you will be able to write typed code and have it run in the browser. However, even then the types won't be checked at runtime. You'll still have to rely on a tool like `tsc` to type check your code.

I’m guessing what’s documented here:

https://www.typescriptlang.org/docs/handbook/jsdoc-supported...

I’ve never written TypeScript myself so just a guess.

There’s also this for node.js:

https://github.com/TypeStrong/ts-node