Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

recently there are a few stories about "using jsdoc for static typing instead of typescript as a JS programmer", I wonder when JS can add support of static typing directly as Python did, that will be nicer without learning a new language like TS.

TS is great, I just never could find time to learn one more language.



The projects that are using JSDoc for typings are doing so because they are way, way out on the edge in ways that stress Typescript compilers. They're doing weird stuff. It isn't a recommended path unless you absolutely need it; it's sacrificing a lot in order to yield faster round-trip time on codebases that have no other option.

Typescript isn't really "a new language", either. It's a pretty mild superset.


> because they are way, way out on the edge in ways that stress Typescript compilers

I don't think this is true at all. Probably the main reason people typecheck js with jsdoc annotations is because they don't want a TS compile step.

Nodejs ES modules, and typescript, make it more complicated that it needs to be to write JS specifically for nodejs/cli scripts (not bundling to run in the browser). I now just prefer to // @ts-check my js files when writing node scripts.


> Probably the main reason people typecheck js with jsdoc annotations is because they don't want a TS compile step.

Right, I agree--because it's slow. Like AFAIK the tooling roundtrip is why Svelte/SvelteKit made the change. For normal projects, you don't have that same problem.

> for nodejs/cli scripts

I just use ts-node with the ESM loader. It's drop-in, for my purposes at least. What problems are you seeing?


The best thing with TS is you can apply as much or as little as you want. Start by adding the compiler to your workflow and get it to work without making any changes to your code. Then add a couple types and see how it goes.


You can achieve the same thing TS does with JSDoc, it's just a lot harder to control it across teams. TS wouldn't be very useful on its own for our usage, but once you couple ESlint with TSconfig and setup your ourganisationwide vsc .settings for TS usesage you can really control the flow of how every developer on your team works with TS. I'm not sure JSDoc has the equivalent to @typescript-plugin rulesets for linting purposes, or if you can build the rules directly into your CI pipeline in a way that prevents rulebreakers from comitting their code.

Aside from that, it's a little different to work with JSDoc. It doesn't have quite the same intellisense integration in VSC. It's also a little different for many developers who aren't necessarily used to writing their documentation first or along with their code, but are used to working with things like C#, Java or similar.

If you want to put in the effort, you can likely setup a JSDoc dev environment for JS where you're getting the benefits of TS without the compiling. I don't see us doing it any time soon, but as someone who's very fond of TS I also wouldn't be surprised if we won't be using it for JavaScript in 5 years.


Python doesn't have static typing until you add a library to check your "hints". This is valid in Python 3.10:

    def take_a_string_and_return_a_boolean(x: str) -> bool:
        return x

    take_a_string_and_return_a_boolean("hello world") # => "hello world"
    take_a_string_and_return_a_boolean(1234) # => 1234
    take_a_string_and_return_a_boolean(None) # => None


Yes, no language has static typing until you run a static typechecker; for some languages you don’t notice because it is integrated into the normal compiler, etc.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: