-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Type definitions for use with Flow #805
Conversation
…tSetValue` After some testing, my feeling is that requiring checks for undefined values returned from methods such as `first()` and `last()` is more restrictive than helpful. There might be a similar problem with `reduce()` and `reduceRight()`. But what is more problematic is that using overloaded signatures for these functions makes it difficult to track down errors in reducer functions, because errors reported by Flow arising from use of overloaded functions tend to lack information.
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
+1 This is very useful. |
@hallettj Keen to get flow working in our codebase at work so I took this for a spin as it's looking promising judging by conversation over at #203. Hit a blocker straight away though with the import {Record} from 'immutable';
const Project = Record({
id: undefined
});
const myProject = new Project({
id: 1
}); will give the error:
Am I missing something extra to get flow working here? I saw the paragraph in #203 about Records but it seems to me like the above (standard way of using records) should still work alongside being abl to generate a stricter type with the desired record shape? I guess this highlights another requirement for this PR... Docs! :) I will update this with any other findings I might make. |
@chrisui Does this work? const Project = Record({
id: (undefined: number|void)
}); |
@samwgoldman yup! So many nifty little tricks hidden away with flow! (okay they may not be "hidden", that "nifty" or "tricks" but to me they are! :)) |
@samwgoldman Is there any way we can support the following? import {Record} from 'immutable';
function createApiRecord(values) {
const NewRecord = Record(values);
return class ApiRecord extends NewRecord {
static TYPE = values.type;
};
}
const Project = createApiRecord({
type: 'project',
id: (undefined: string|void)
});
const myProj = new Project({
id: '1'
}); Current errors:
The purpose of this is to provide some introspection to our records. Ie. nicer to do |
@tsing: Thanks for the feedback! I don't use the cursor feature. (I prefer to use my own lens library.) I'm not inclined to write type definitions for a module that I do not plan to use. But if you or someone else want to add definitions for cursor types, I think that would be done by creating a file |
This is so dope. Thanks for your hard work! |
Speaking of which... are there any? :) How are Records supposed to work? For the following file:
I only get a single error:
I've also tried the syntax outlined in #203 (comment), but it doesn't seem to fair any better in catching these issues. Is Record support just not there yet?
|
First off, thank you so much for that work, it is super amazing to have flow definitions in immutable js now! With the flow definitions that were floating around previously we could define a record as an ES6 class and have flow correctly typecheck it. Now with immutable 3.8.0 and flow 0.23.0 this doesn't work any more. /* @flow */
import { Record } from 'immutable'
export default class Person extends Record({
name: null,
email: null,
}) {
name: string;
email: string;
}
Can anyone point me in the right direction in order to fix this? |
@jareware: There are notes on the limitations of record type-checking in this comment from the discussion on #203. That is certainly not the best place for that information. |
@MSch : Thanks for the heads-up! I am looking into it. |
Hello, I am a little lost in all the comments and versions. With the current flow and current head of your fork, how do the Records work and what they check and what they don't check? Why is, for example, the Record signature
? When I uncomment the annotation as
it seems to work better. |
Also, shouldn't Iterable's (and therefore, Map's)
instead of this
? Flow's Map has this
I can't tell what's the difference between |
@Runn1ng wrote:
I put in the In the meantime you can get that type by putting explicit annotations in your code: const MyRecord = Record({ /* whatever structure `MyType` has */ })
const myRecord: MyType & Record<MyType> = new MyRecord({ /* whatever */ }) Btw, @marudor, I love the changes that you made to the type definitions! I'm going to have to remember that trick with type parameters on
I agree that using
Those types are nearly the same. I believe that Edit: My Record example was not actually correct. |
Thanks for the comments!
Hm. What I do like about Flow is that it doesn't have partial function types and I cannot accidentally have undefined/null by mistake. And I am not sure about the precedent - as I said, in Flow's own Map, the returning type is But otherwise the definition file is great, I am using it right now. |
This pull request includes type definitions, and adds a step to the build process to copy the type definitions to
dist/
. With the definitions file indist/
, users who import Immutable and who use Flow will pick up the type definitions automatically.This is intended to address issue #203.
An important caveat when using these definitions is that the types for
Iterable.Keyed
,Iterable.Indexed
,Seq.Keyed
, and so on are stubs. When referring to those types, you can get the proper definitions by importing the typesKeyedIterable
,IndexedIterable
,KeyedSeq
, etc. For example,This is because I have not been able to work around issues Flow has with a class holding a reference to its own subclasses, and with overriding static properties with values that have different types. (E.g.,
Seq.Keyed
is not the same type asIterable.Keyed
, butSeq
is a subclass ofIterable
.)