Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compact type annotation syntax that converts to JSDOC AST #49175

Closed
5 tasks done
ftaiolivista opened this issue May 19, 2022 · 4 comments
Closed
5 tasks done

Compact type annotation syntax that converts to JSDOC AST #49175

ftaiolivista opened this issue May 19, 2022 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@ftaiolivista
Copy link

Suggestion

🔍 Search Terms

jsdoc

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I regularly use Typescript to check my vanilla JS with the help of JSDOC blocks.

But I find JDOC syntax, especially when grows in complexity, very bloated. If you extensively use it, easy that JSDoc rows outnumber by 2x ore more code rows.

My suggestion is implement in TS parser a convention to annotate types in a more compact way

📃 Motivating Example

Even for simple cases like this:

/**
 * @type {string}
 */
var s;
 
/** @type {Window} */
var win;
 
/** @type {PromiseLike<string>} */
var promisedString;
 
/** @type {HTMLElement} */
var myElement = document.querySelector(selector);
element.dataset.myData = "";

/**
 * And arrow function expressions
 * @param {number} x - A multiplier
 */
let myArrow = (x) => x * x;

/** @type {(s: string, b: boolean) => number} TypeScript syntax */
var sbn2;

I think a more compact and efficient sintax can help to make the code more readeable, something like this:

//: string
var s;
 
//: Window
var win;
 
//: PromiseLike<string>
var promisedString;
 
//: number => number
let myArrow = (x) => x * x;

//: (string, boolean) => number
var sbn2;

One can take the convention that the shortcut "::" (for example) means @type ....

One other nice to have would be the possibility to directly embed d.ts code block in the .js file. I think this would be better than have complex JSDoc blocks like @typedef or @extends.

/*::
    export type ArrayMetadata<ArrType> = {
        length: number;
        firstObject: ArrType | undefined;
    };
    export function getArrayMetadata<ArrType>(
        arr: ArrType[]
    ): ArrayMet
*/

This could be implemented in Typescript parser. The new compact definition blocks could be parsed and automatically transformed to the corrispondend JSDOC AST.

💻 Use Cases

Write better readeble JS type hints and declaration
do not have separate d.ts for JS

P.S. interesting discussion for JS proposal

@MartinJohns
Copy link
Contributor

IMO it's a duplicate of #9694. Just a different syntax.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label May 31, 2022
@andrewbranch
Copy link
Member

andrewbranch commented May 31, 2022

Also, at this point, the solution we’re pursuing is bringing type annotations to JS. We’d like to avoid inventing a brand new bespoke comment syntax while that proposal is alive.

@ftaiolivista
Copy link
Author

Also, at this point, the solution we’re pursuing is bringing type annotations to JS. We’d like to avoid inventing a brand new bespoke comment syntax while that proposal is alive.

Personally I don't like the proposal. The TS like inline syntax make the codebase less clear to me. I'm more for keep type hints apart form code. But probably it's only my preference

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants