Apologies for seemingly being a bit argumentative. I'd just like people to recognise both the strengths and weaknesses of JS vs TS.
Are you running the node through a TS compiler? I know that Node.js is introducing "native" TS execution but I believe that they are "simply" (not actually simple at all I'm sure) removing the type annotations at execution time?
Not sure about interface definitions. Personally, I've always found those to get in the way rather than help but then I have never been a professional JS/TS programmer in an enterprise environment where I think that they might actually be helpful rather than just an overhead.
Custom types can, of course, be defined in JSDoc quite easily.
Would you mind explaining this one? I'm not sure what you mean.
This is certainly easier to do in TS than with JSDoc, that's for sure.
This should work, it works in VS Code:
/**
* @typedef {object} Page
* @property {string} path - File path of the page
* @property {string} name - Generated name of the page (format: "pagesN.jpg")
* @property {number} width - Width of the page in pixels
* @property {number} height - Height of the page in pixels
* @property {number} bytes - Size of the page in bytes
* @property {string} type - MIME type of the page (always "image/jpeg")
*/
const pages = (msg.payload)
.filter( l => 1.trim())
.map( (line, i) => {
const [path, width, height, size] = line.split(' ; ')
/** @type {Page} */
return {
path,
name: `pages${i + 1}.jpg`,
width: Number(width),
height: Number(height),
bytes: Number(size),
type: 'image/jpeg'
}
})
pages[0].name = 2
In VS Code it does give the realtime error. But I think it only works there because it needs the ESLINT extensions and config.
Urm, actually, you don't need the type definition, it works without as long as ESLINT is configured correctly.
That seems like an interesting idea.