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

Out of Band Type Annotation Support for Signatures #11218

Closed
iclanzan opened this issue Sep 28, 2016 · 10 comments
Closed

Out of Band Type Annotation Support for Signatures #11218

iclanzan opened this issue Sep 28, 2016 · 10 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@iclanzan
Copy link

I am not sure if it would be at all possible or feasible but I would really appreciate Hindley–Milner type signature support in TypeScript:

// stringLength :: String -> Number
const stringLength = word => word.length

I am writing a lot of code like this as I find it much easier to read but unfortunately TypeScript doesn’t understand this notation.

Some relevant reading:

@DanielRosenwasser DanielRosenwasser changed the title Hindley–Milner Type Signature Support Out of Band Type Annotation Support Sep 28, 2016
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Sep 28, 2016

What's the distinction vs this? Just the syntactic location, or the actual form of the signature type?

const stringLength: (s: string) => number = word => word.length;

@DanielRosenwasser
Copy link
Member

Changing the title, since it doesn't actually have anything to do with Hindley-Milner which is an inference algorithm, not a syntactic style. Standard ML, for instance, doesn't use this syntax.

@DanielRosenwasser DanielRosenwasser changed the title Out of Band Type Annotation Support Out of Band Type Annotation Support for Signatures Sep 28, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Sep 28, 2016

this would be my recommendation:

const stringLength = (word: string) => word.length;

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Sep 28, 2016
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Out of Scope This idea sits outside of the TypeScript language design constraints and removed Question An issue which isn't directly actionable in code labels Sep 29, 2016
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Sep 29, 2016

I don't see enough of a motivation to depart from the ECMAScript-reserved syntax, especially given that there's a sufficient mechanism in place that doesn't have any specific problems. For what it's worth, this also took me a little getting used to when I moved from Haskell to Standard ML. Thanks for filing though!

@iclanzan
Copy link
Author

Wow, this was closed fast. I was hoping for more discussion. I know there are other TypeScript users that would like to see something like this supported.

What's the distinction vs this? Just the syntactic location, or the actual form of the signature type?

  const stringLength: (s: string) => number = word => word.length;

I find something like the above very confusing/hard to read, and this is for the most basic example.

Changing the title, since it doesn't actually have anything to do with Hindley-Milner which is an inference algorithm, not a syntactic style.

Hindley-Milner is a type system, which does describe an inference algorithm, but also comes with a syntax which functional languages like Haskell and functional JavaScript libraries have adopted to use as type annotations.

I don't see enough of a motivation to depart from the ECMAScript-reserved syntax.

What do you mean by “ECMAScript-reserved syntax”? Since when are these type annotations ECMAScript-reserved syntax? With my proposed annotations I can use regular ECMAScript which allows me to use it in projects where the move to the TypeScript syntax is not desired.

@ghost
Copy link

ghost commented Sep 29, 2016

Good you came back and explained further as I completely misunderstood your question in the first instance. Sure, this syntax is very familiar to Haskellers

// stringLength :: String -> Number
const stringLength = word => word.length

and, IIRC, Elm (compile-to-JS) language also makes use of the same .. so you are certainly not alone in having some affinity for it.

I'm just a casual observer though does seem like your issue was closed off & dismissed rather quickly. Maybe for the moment you will just have to accept putting your style of type annotations in alla-comment-style, which, btw, is something that I understand Flow people subsequently embraced.

@iclanzan
Copy link
Author

iclanzan commented Sep 29, 2016

I am totally fine and actually prefer putting my type annotations in comments. The problem is I get no help from TypeScript using these type signatures.

@masaeedu
Copy link
Contributor

masaeedu commented Aug 21, 2017

Today you can write:

type StringLength = (s: string) => number
const stringLength: StringLength = word => word.length

@naholyr
Copy link

naholyr commented Oct 10, 2017

Not even close :/

@seanpoulter
Copy link

Has anyone found a way to support these type annotations? I'm jumping into a JavaScript project using these annotations and would love to support these type annotations in comments like the original post above.

This seems like a good fit for a TypeScript Language Service Plugin.

cc @iclanzan

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants