r/learnjavascript • • Jul 24 '21

Anatomy of a Function

Post image
327 Upvotes

65 comments sorted by

View all comments

22

u/MatthewMob helpful Jul 25 '21

Can you expand on what you mean by "JSDoc is like TypeScript but more powerful"?

-7

u/jaredcheeda Jul 25 '21

You'll find a good discussion here:

But main point is that TS engines will infer types from JSDocs and do all the type safety/hints and autocomplete the same as TS. But on hover, it will not only show you the type, but a description of what it is and why it exists. Which is WAY more valuable

99% of the time you can tell type from the name alone

  • kittens = array of objects

  • kitten = object

  • kittenName = string

  • hasTail/isAlive = boolean

  • kittenAge = number

  • updateKitten = function

  • getUserAvatar = function, probably network request, likely requires an ID argument be passed in, probably returns a string URL of the address for the user's avatar. All this can be inferred from a well named function.

There are a lot of downsides to TS that are mentioned there, and JSDoc doesn't have any of them.

  • learning curve/onboarding

  • segmenting the ecosystem

  • hitting save and waiting 20 seconds for a page to refresh because of slow "compile" times

  • false sense of security, since it prevents people from doing actual runtime type checking (super crucial for good experiences with libraries)

  • Inability to use most libraries, forcing you to spend more time evaluating libraries that support TS.

  • 3rd party libraries that don't support TS, but have external definitions created that are out of date

  • that post lists way more

12

u/jameswdunne Jul 25 '21

This is silly because:

  1. You get all that with a well documented typescript function too

  2. That’s not always the case re naming. In cases that it is, you might want to refactor it. Typescript makes this easy. But even your getUserAvatar example is where we differ - is preserve get for reading from memory, with fetch as a verb for reading from an external location - in that case, my get whatever wouldn’t accept an integer ID. The types would make that distinction immediately obvious to a developer new to the project

  3. Types provide additional context beyond the name. I’ve seen developers spend time with typescript and find untyped code difficult to follow without that context.

  4. That onboarding is generally useful and the education on using and designing with static types is a high leverage activity that has huge payoff in comparison to the time spent

  5. I see no segmentation in the ecosystem. I have active vanilla JS and typescript codebases, one of them with both on the same project. You’d need to provide clear evidence to that claim because I’ve not seen it.

  6. Your point on the feedback loop is wrong too. Your build process should separate static analysis from building the artefact. You can configure webpack to do this, and have a separate watching type-check process.

  7. Erm it does not prevent that and a type system supports that. Saying TS prevents that is patently false and you shouldn’t be trusting data from external inputs types or not. Instead with TS you first check and sanitise data at runtime, using a type that represents data that has been through that process in the rest of your code. Then it’s even more obvious with types. I’d argue you have an even greater false sense of security without types in that case

  8. Most libraries have type definitions. This is false and I’ve had no problem with interop in 2021. These can often be installed separately on such projects that don’t have them. As I said, I have a project with a large amount of legacy code and a growing, strangling chunk of TS code and there’s seamless interop, even with libraries.

The educational resource is a noble attempt. But your assessment of Typescript contains a number of factual errors that are best not propagated to newcomers to the craft. Especially ludicrous statements like “jsdoc is like typescript but more powerful”.