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

WIP/RFC - first jab at implementing Flow-style annotations-in-comments #13390

Closed
wants to merge 1 commit into from

Conversation

akdor1154
Copy link

@akdor1154 akdor1154 commented Jan 10, 2017

See #9694 .

My workplace, and many community projects, are aware of the analysis capabilities of Typescript, but shy away from having a build step added to projects that are currently written in raw JS. This makes Typescript a non-starter. Facebook's Flow has a nifty feature whereby annotations can be added as comments inline in JS, meaning no build step is required. E.g.

const s /*: string */ = 'asdf';

function f(s /*: string */) /*: number */ {
    return 4;
}

/*::
interface MyObject {
    s: string;
    n: number;
}
*/
const o /*: MyObject */ = {s: 'asdf', n: 123}

This seems to have seen wide approval and support from the Flow community.

This PR implements this feature in Typescript. It's clearly uglier that direct annotations, but it's a much more succinct way of adding Typescript annotations to valid Javascript than JSDoc.

Two constructs are supported. /*: typename*/ is interpreted as : typename, as in the first two examples above. /* :: statements; ... */ is interpreted as statements; ....

It may also be worth considering /* <parameter> */ -> <parameter> to allow manually specifying generic parameters where elision is not possible. This would also allow angle bracket type assertions. Note that this would be a convenience extension - as currently implemented, this is already possible with /* :: <parameter> */

This is a first go to see how easy this was to implement - I am aware that the CLA needs to be signed, test cases need to be written, that the issue needs to be tagged as needing a proposal (and that there is no guarantee this will happen!), and said proposal needs to be written and accepted, and that this is a language change and will thus be either highly contentious or rejected outright.

@msftclas
Copy link

Hi @akdor1154, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla.microsoft.com.

TTYL, MSBOT;

@pardonmyenglish
Copy link

Two constructs are supported. /*: typename*/ is interpreted as : typename, as in the first two examples above. /* :: statements; ... */ is interpreted as statements; ....

I assume you mean /*:: statements; ... */ (no space between the asterisk and colons)

@akdor1154
Copy link
Author

Sort of - parsing is tolerant of whitespace, matching /*:..., /* :..., /* :..., etc.

@mhegazy
Copy link
Contributor

mhegazy commented May 22, 2017

Please see https://github.com/Microsoft/TypeScript/wiki/Type-Checking-JavaScript-Files for relevant information.

@mhegazy mhegazy closed this May 22, 2017
@akdor1154
Copy link
Author

akdor1154 commented May 23, 2017

:( Though I really appreciate the thorough JSDoc support, I find Typescript's native type expressions to be far more legible.
There are things that are far easier to express in Typescript's type system than in JSDoc (i.e. interfaces' interplay with discriminators and type-guards). I hope the team revisits this one day.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants