JSMonk / hegel

An advanced static type checker
https://hegel.js.org
MIT License
2.09k stars 59 forks source link

Types as comments syntax? #387

Open piranna opened 1 year ago

piranna commented 1 year ago

Hegel inference system is really powerful, so types are usually not needed, but some times they are. For that, a Typescript / Flow syntax is being used, that later needs to be removed. In the same spirit of type annotations proposal where types syntax should be treated as comments by Javascript runtimes so there's no need of transpilation, what about if we add a comments-based syntax so we don't need it either? Two options I find here are respect JsDocs when they are defined, that are pretty common but verbose, or allow to use inline comments. This can be verbose too, so to minimize them, I propose to use inline comments, and consider them Hegel types if the first token inside the comment would be a valid Hegel type token:

const num /*:number*/ = 42;

function func(a/*:number*/, b/*:number*/)/*:number*/
{
  return a + b
}

/*
type A
{
  name: string
}
*/

class B /*implements A*/{}

Syntax would need to be developed in more detail, but you get the idea, and I think that would be the most minimal solution.

piranna commented 10 months ago

I have just discover this syntax and intention are pretty much similar to Flow inline comments, so I think it would be a nice addition :-)