mackuba, to random
@mackuba@martianbase.net avatar

I added

/**

  • @types 😬
    */

( in mode)… https://github.com/mackuba/skythread/compare/ae408616...master

I wonder how useful and how annoying it will turn out to be. In any case, deleting them would be a few clicks in the editor. But it looks not terrible and it should at least make refactoring safer 🤔

aral, to javascript
@aral@mastodon.ar.al avatar

If you use a parameter object in JavaScript, even if you specify its shape using JSDoc, you’ll only get errors if required properties are missing; not if there are extra properties provided.

e.g.,

class A {  
 /**  
 @param {{  
 id:string  
 }} params  
 */  
 constructor (params) {  
 Object.assign(this, params)  
 }  
}

// Error:  
new A({})

// No error:  
new A({id: 'x', foo:'bar'})  

According to this thread, it’s a fact-of-life:

https://stackoverflow.com/questions/49580725/is-it-possible-to-restrict-typescript-object-to-contain-only-properties-defined

ondrejsevcik, to typescript
@ondrejsevcik@hachyderm.io avatar

For many projects, JSDoc may be better choice than TypeScript. Less tooling, less complexity, no build step, but still have the benefit of types.

https://alexharri.com/blog/jsdoc-as-an-alternative-typescript-syntax

tomayac, to typescript
@tomayac@toot.cafe avatar

People have posted about using as a alternative for years, but this post by Alex Harri Jónsson seems to be the ultimate cheat sheet: https://alexharri.com/blog/jsdoc-as-an-alternative-typescript-syntax.

nurkiewicz, to til
@nurkiewicz@fosstodon.org avatar

can type-check files (to extent it's possible) with allowJs and checkJs compiler options. Moreover, it even takes hints from . This makes gradual migration to TS even more compelling as we get type feedback even for existing JS files

VinceAggrippino, to typescript
@VinceAggrippino@techhub.social avatar

I'm using type checking with in client-side and I encountered this problem:

> Property 'innerText' does not exist on type 'Element'

It took a bit of searching, but I've learned that the solution for me is:

/** @type@techhub.social {NodeListOf<HTMLElement>} */  

I don't like how this is documented. It's on a page about DOM manipulation with TypeScript, which makes some sense, but I didn't see anything about doing this in JS with JSDoc. It says it's defined in lib.dom.d.ts and I thought I might have to import a type definition somehow.

I found it here:
https://www.typescriptlang.org/docs/handbook/dom-manipulation.html#the-queryselector-and-queryselectorall-methods

VinceAggrippino, to javascript
@VinceAggrippino@techhub.social avatar

When type checking using in it treats return types as optional... no return value, no error

Same when using tsc on the command line.

However, if you set up the Playground to check JavaScript, it works like it does with TypeScript.

VinceAggrippino, to javascript
@VinceAggrippino@techhub.social avatar

You can argue for learning the fundamentals and using "Vanilla" all day long... I'll probably join you... but using and types in JS is forcing me to learn the HTML DOM and Web APIs better.

In the attached screenshot, for example, the correct type isn't the inferred one of Element, but HTMLInputElement: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement

dandb, to php
@dandb@mas.to avatar

The way PhpStorm deals with imported typedefs (eg importing from a declaration file) leaves something to be desired.

Right now intellisense just doesn't really work, though validation does.

Maybe I'm missing some kind of config or just doing it wrong or something?

schizanon, to javascript
@schizanon@mas.to avatar
danrot, to typescript
@danrot@mastodon.social avatar

I am trying to use on normal files using , and I have to say I like it so far. It shows me errors and provides good auto completion and all of that without a build step. Just wrote a few lines yet, therefore I have to ask: Does anyone know any downsides of this approach? 🤔

danrot,
@danrot@mastodon.social avatar
aral, to javascript
@aral@mastodon.ar.al avatar

If you’re using JSDoc to cast, say, a global object to a class, here’s how you do it (TL; DR: Use typeof – it wasn’t immediately clear to me).

https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/66713#discussioncomment-7043990

aral,
@aral@mastodon.ar.al avatar

Yeah, I have no idea how this is still an issue in JSDoc in 2023. It looks like the project is abandoned – which is a damn shame for those of us who want type safety without having to introduce a build step using TypeScript in our projects.

https://github.com/jsdoc/jsdoc/issues/1349#issuecomment-1725400751

schizanon, to typescript
@schizanon@mas.to avatar

It's incredibly validating to me to hear everyone questioning .

I've been hating it's guts for like 4 years now, saying that people should just use

Glad y'all are finally getting off the hype-train!

aral, (edited ) to random
@aral@mastodon.ar.al avatar

This took me way too long to figure out…

If your function takes a parameter object and uses spread syntax to collect arbitrary additional properties (rest) and you want to document it using , the syntax is:

e.g.,

/**  
 @param {{  
 state: import('./SetupState.script.js').default,  
 [p:string]:any  
 }} parameterObject  
 @returns {string|string[]} html  
*/  
export default ({state, ...props}) => html`…`  

Solution by Alexander Lonberg https://stackoverflow.com/a/68567492

aral, to javascript
@aral@mastodon.ar.al avatar

A little tip:

If you need to use a type definition in an external file multiple times, you don’t have to keep writing import(…).type over and over. Just create a new type definition in your local file and use that.

e.g.,

/** @typedef {import('./SetupState.js').SetupStateType} SetupStateType */

const state = {  
 domain: /** @type SetupStateType */ (new SetupState('Domain')),  
 server: /** @type SetupStateType */ (new SetupState('Server')),  
 …  
}  

macdonst, to typescript
@macdonst@mastodon.online avatar

🚫 TypeScript: YAGNI

TypeScript! What is it good for? I wouldn’t go as far as to say “absolutely nothing” like the song. I’m not trying to start a war here. It certainly is widespread, with JetBrains estimating it has nearly tripled in popularity over the past six years. However, we are scientists and using popularity as a reason for adopting a new tool is an excellent way to commit a logical fallacy.

https://begin.com/blog/posts/2023-07-19-typescript-yagni

rach, to random

📝 new note: is TypeScript good? 🤔 https://rachsmith.com/is-typescript-good/

vintprox,
@vintprox@techhub.social avatar

@grooovinger @castastrophe @kizu @rach

Since generics were not working very well in 6 months ago, I still have this lingering feeling that tossing right away in favor of JSDoc is going to hurt some projects. If the situation with "templates"/generics has been resolved since then, then I would be able to seriously suggest moving to such opt-in commenting.

schizanon, to javascript
@schizanon@mas.to avatar

I love it when a library lets you choose between and in the code examples, but I wish that they would include the types in the version since they don't hurt anything and they are still helpful when you're using

ricard, to javascript
@ricard@ricard.social avatar

:javascript: moving from TS to JSDoc, love it 💜

➡️ https://github.com/sveltejs/svelte/pull/8569

Comment from Rich Harris: https://news.ycombinator.com/item?id=35892250

vintprox,
@vintprox@techhub.social avatar

@ricard

It's an interesting route, and I didn't see much projects that would move away from to type hints. I tried writing generics in it some months ago and didn't find this experience pleasing at all. But I hope this doesn't serve as a limitation for making type-safe.

aral, to programming
@aral@mastodon.ar.al avatar

Want strong typing without a build process?

(Hint: you don’t need to use TypeScript.)

  1. Use a modern editor like Helix Editor or VSCodium that supports the TypeScript Language Server (LSP).

  2. Add this line to the top of your JavaScript file (without the backticks, if you see any):

// @ts-check  
  1. Go read up on JSDoc ;)

¹ https://helix-editor.com
² https://jsdoc.app/about-getting-started.html

aral, (edited )
@aral@mastodon.ar.al avatar

PS. Some :

You can refer to types in other modules & annotate parameters inline.

async createOrUpdate (/** @type import('stripe').Stripe */ stripe) { … }  

You can define custom types, use unions:

@typedef {State & RegistrationFormProperties} RegistrationFormStateType  

You can cast (note the parentheses):

state = /** @type RegistrationFormStateType */ (new RegistrationFormState())  

For more advanced uses, see Closure Type System: https://github.com/google/closure-compiler/wiki/Types-in-the-Closure-Type-System

aral, (edited ) to programming
@aral@mastodon.ar.al avatar

SystemError typedef for Node.js (gist)

https://codeberg.org/aral/gists/src/branch/main/SystemError.md

Node.js doesn’t expose the SystemError class in any way. This JSDoc type definition works around this limitation.

Update: Yeah, Node.js should really just expose the SystemError class because this is ridiculous.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • JUstTest
  • kavyap
  • DreamBathrooms
  • tacticalgear
  • mdbf
  • InstantRegret
  • magazineikmin
  • Youngstown
  • thenastyranch
  • rosin
  • slotface
  • Durango
  • cubers
  • ngwrru68w68
  • anitta
  • cisconetworking
  • GTA5RPClips
  • modclub
  • tester
  • khanakhh
  • everett
  • provamag3
  • osvaldo12
  • Leos
  • normalnudes
  • ethstaker
  • megavids
  • lostlight
  • All magazines