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

more documentation #615

Closed
nmn opened this issue Jul 8, 2015 · 11 comments
Closed

more documentation #615

nmn opened this issue Jul 8, 2015 · 11 comments

Comments

@nmn
Copy link
Contributor

nmn commented Jul 8, 2015

The documentation for flow hasn't been updated much in a long time, and the project itself has moved quite a bit. I think it would be great to have a section explaining these parts in more detail —

  1. how to annotate functions as value. var x : (number) => string
  2. How type imports and exports work. import type x from 'y'
  3. Documentation covering unions and the recently added disjoint unions.

I'd be more than willing to create pull request if you can help me figure out 2.
(short answers here, or links will do) :)

@nmn nmn changed the title more documentation for declarations more documentation Jul 8, 2015
@popham
Copy link
Contributor

popham commented Jul 24, 2015

Since you're importing, I'll assume that you're using ES6 modules. On (2), to type x from 'y', there must be a y.js with

export default class K {...} // I would import this with a capital letter for its alias
// or
export default function k {...}
// or
export type k = number | { someKey: string };
// or
export var k = 5; // I'm not sure if this works; `x` would alias number

If you're playing around, I would suggest relative imports, i.e. import x from './y'. I recently arrived at #644 (comment) for absolute imports, if that helps.

On (3), don't forget about intersections. I've had some success with intersecting classes against objects to impose: (1) is an instance of the class and (2) satisfies the structural constraints of the object. (In my use case, obj & cls works, while cls & obj does not.)

@nmn
Copy link
Contributor Author

nmn commented Jul 26, 2015

@samwgoldman Thanks for the help. I recently managed to use type imports and exports at work. Though most of them were named imports, I can try export default for more clarity. (One reason for my confusion was simply that I was using an older version of Babel)

On (3) I've personally not used too many class types in practice. Instead I define all classes as objects instead. So I will do some more research on that as well.

In regards to that, could you be so kind to also tell me something particular about declarations? Let's say, I have a commonJS project that happens to have a global variable called OT. window has a type definition already defined in flow. Is there a way for me to do declaration that would 'extend' that declaration.

To say something like. This object is of type T but with these additions (modifications)?


Thanks for the help though. I'll make a PR with some documentation soon.

@popham
Copy link
Contributor

popham commented Jul 27, 2015

See #396 for the non-answer to your window question. See #31 (comment) for a helpful suggestion.

@nmn
Copy link
Contributor Author

nmn commented Jul 27, 2015

I just read some more documentation, and it turns out that what I really want is Classes, and extends.
I was a little confused about how typeof worked in flow, but I just figured it out.

@nmn
Copy link
Contributor Author

nmn commented Jul 28, 2015

About intersections, I did some research and an intersection between a class and an object works either way. Though it is a little strange to actually use in practice. It ends up making a smaller object. A union seems to be the correct tool for what you were describing.

@popham
Copy link
Contributor

popham commented Jul 28, 2015

No, I mentioned the 'extends from' and 'implements' constraint. As long as your interface doesn't have any static members, you can have implements support by leveraging intersections. (I'm currently working on a straw man that adds statics to mix--I expect that "proper" interfaces can desugar to this.)

@nmn
Copy link
Contributor Author

nmn commented Jul 28, 2015

@popham I'm probably still confused about how intersections works exactly. I'll keep reading.

@nmn
Copy link
Contributor Author

nmn commented Jul 31, 2015

In #679 I ran into a limitation in Flow. It turns out it's been covered elsewhere in: #282
In both issues, there is a simple workaround for the problem.

Another common issue I run into is with decorator functions on React Classes:

class X extends React.Component{
  render(){...}
}
export default X

exporting X asks you for annotations. Then these annotations help flow catch errors across module boundaries.

However doing this instead erases all those errors:

class X extends React.Component{
  render(){...}
}
export default decorate(X)

Now flow has no idea what the type of the export is, and any type benefits across module boundaries are lost.

The workaround to fix this problem is simple as well:

class X extends React.Component{
  render(){...}
}
var toExport: typeof X = decorate(X)
export default toExport

So my question is, is it a good idea put a collection of these common workarounds in the documentation. Some of them may not be needed in the near future.

@samwgoldman
Copy link
Member

The heap refinement thing should absolutely be documented. Maybe someone will get really clever and limit the scope of the refinement havoc, but I don't think it'll happen any time soon, and when it does we can update the docs.

The second issue you identified might be a bug. You mentioned it in #520 (comment) but I think it's worth opening an issue, because it's separate from autocomplete. I don't think anyone has really looked into the behavior you saw, so I wouldn't document that yet.

@samwgoldman
Copy link
Member

I spent a bit of time improving (hopefully) the documentation around dynamic type tests and refinements. I added a "caveats" section that describes the scenarios in which flow will throw away a refinement. Hopefully we can use that to stem the tide of issues related to that behavior. Ref #711.

@jeffmo
Copy link
Contributor

jeffmo commented Jun 23, 2016

Closing per @samwgoldman's docs improvements

@jeffmo jeffmo closed this as completed Jun 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants