Skip to content

Flow generics in comments #4706

@jamiebuilds

Description

@jamiebuilds

Flow supports generic types using the comment syntax in a couple key locations:

/* @flow */

function identity/*::<T>*/(value /*: T */)/*: T */ {
  return value;
}

class Container/*::<T>*/ {
  method/*::<U>*/() {
    return true;
  }
}

let arr = [1, 2, 3];
let copy = arr.map(/*:: <T> */(item /*: T */) /*: T */ => item);

But the following roughly equivalent CoffeeScript code:

# @flow

identity = ###::<T>### (value ###: T ###) ###: T ### ->
  value

class Container ###::<T> ###
  method: ###::<U> ### () -> true
  
arr = [1, 2, 3];
copy = arr.map(###:: <T> ###(item ###: T ###) ###: T ### => item)

Which compiles to:

// @flow
var Container, arr, copy, identity, val;

identity = function(/*::<T>*/value/*: T */)/*: T */ {
  return value;
};

Container/*::<T> */ = class Container {
  method() {
    return true;
  }

};

arr = [1, 2, 3];

copy = arr.map((/*:: <T> */item/*: T */)/*: T */ => {
  return item;
});

If CoffeeScript put the comments in the correct location for Flow generics, it would cover the entire Flow grammar 1-to-1

  // @flow
  var Container, arr, copy, identity, val;

- identity = function(/*::<T>*/value/*: T */)/*: T */ {
+ identity = function/*::<T>*/(value/*: T */)/*: T */ {
    return value;
  };

- Container/*::<T> */ = class Container {
+ Container = class Container /*::<T> */ {
    method() {
      return true;
    }

  };

  arr = [1, 2, 3];

- copy = arr.map((/*:: <T> */item/*: T */)/*: T */ => {
+ copy = arr.map(/*:: <T> */(item/*: T */)/*: T */ => {
    return item;
  });

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions