3 ms·
The author seems to ignore the fact that type hints are a thing even with plain JS. Serious question: does the author "just say no to Python" as well? If lack
by qayxc 2y ago
The author seems to ignore the fact that type hints are a thing even with plain JS. Serious question: does the author "just say no to Python" as well?
If lack of explicit typing is the issue, then his rant would apply to most scripting languages and to single out JS seems odd to me...
- glutamate 2y ago> type hints are a thing even with plain JS Can you clarify how you do this? the type annotation TC39 is not approved; but I'm interested in hearing if you have a "userland" approach that works for you?
- jeroenhd 2y agoI've seen several ways of annotating Javascript that IDEs seem to understand. They usually involve using comments before fields, classes, or functions. The most compliant one seems to be using [JSDoc](https://jsdoc.app/ https://jsdoc.app/). JSDoc is mostly intended for generating documentation. However, the Typescript compiler can validate types (and can even interoperate with Typescript definitions), if you configure it as such. In scenarios where you HAVE to write raw Javascript but still would like to do some type validation, this is probably the best solution. It looks a bit like this: /** * Fiddle the widget * @param {WidgetThing} The widget to fiddle */ function fiddleWidget(widget) { This is a type hint that asserts that `widget` is of type `WidgetThing`. JSDoc also works in the middle of a method: /** @type {Foo} */ const foo = widget.fetchFoo(); This asserts that `foo` is of type `Foo`. This could in theory also be derived from the return value of `widget.fetchFoo()` of course. JSDoc also has arbitrary type definitions: /** * @typedef Foo * @type {object} * @property {WidgetThing} parent - the parent that created this Foo. * @property {string} name - the name of this Foo. */ I would stick to transpiling Typescript myself, but I've also seen use cases where that's simply not an option.
- steinuil 2y agoPython actually has a good typed ecosystem these days, and unlike JS -> TS you don't have to add a compilation step to benefit from it: you just have to enable type checking in your IDE. JS is in a different spot where to benefit from type checking you have to add a build step or reconfigure your build tool, switch files to another extension, add some "type stubs" dependencies, and make significant changes to the codebase to benefit from type checking. I think if Node and the browsers started allowing type hints in their parsers you'd see much less resistance to TypeScript. Other scripting languages are probably not popular enough to matter as much as JS does.
- qayxc 2y ago> you don't have to add a compilation step to benefit from it: you just have to enable type checking in your IDE. It's similar for JS, believe it or not. Type hints via jsdoc can be just as easily configured for type-checking in our IDE of choice. Python type hints aren't mandatory either, so they're strictly opt-in and nothing stops users from ignoring them.
- steinuil 2y agoI know, I've used jsdoc type hints and I don't like them very much. They're verbose, types and names are the wrong way around, there's about two or three different syntaxes for defining any one thing, you don't get any autoformatting (AFAIK)... I'm not a fan.