-
-
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
Update Record Flow Type Definition #962
Conversation
@leebyron do you know if ImmutableJS is being maintained? |
@wokalski thanks! I'll check em out |
I'd love to see this implemented as well. I just setup Flow in my project, and it is counterproductive to keep seeing warnings that |
@ccorcos Yeah we are maintaining this now all seriously-like. I was thinking it would be nice when we update flow types to make sure they are exercised in a test (for the withMutations part) - does that seems sane? Or is that already happening somewhere. |
@lacker flow-typed seems to be the new place to maintain 3rd party lib defs -- they have test setups and everything... |
OK so #878 is in now so we have some Flow type refactorings and also you can now test flow types. Would you mind updating to merge those changes in, and also adding a test that catches these changes? Thanks! |
Here's something interesting I've been experimenting with in my own code. It's painfully verbose, but it typechecks extremely well. // @flow
import {Record as iRecord} from 'immutable'
declare class RecordAPI<T> {
constructor(init?: $Shape<T>): void;
get<A>(key: $Keys<T>): A;
set<A>(key: $Keys<T>, value: A): Record<T>;
hasIn(keys: Array<any>): boolean;
update<A>(key: $Keys<T>, updater: (value: A) => A): Record<T>;
updateIn<A>(path: Array<any>, updater: (value: A) => A): Record<T>;
merge(values: $Shape<T>): Record<T>;
withMutations(mutator: (mutable: Record<T>) => any): Record<T>;
inspect(): string;
toObject(): T;
}
export function Record<T: Object>(spec: T): Class<RecordAPI<T>> {
return iRecord(spec)
}
type AddressInit = $Shape<{
street: string,
city: string,
zip: string,
}>
class Address extends Record(({street: '', city: '', zip: ''}: {
street: string,
city: string,
zip: string,
})) {
street: string
city: string
zip: string
}
type UserInit = $Shape<{
name: string,
address: AddressInit,
}>
class User extends Record(({name: '', address: new Address()}: {
name: string,
address: Address
})) {
name: string
address: Address
constructor({name, address}: UserInit = {}) {
super({name, address: new Address(address)})
}
}
const user = new User({
name: 'Andy',
address: {
street: 'wherever',
city: 'Austin',
zip: 'TX'
}
})
user.set('name', 2)
console.log(user)
console.log('user.address instanceof Address:', user.address instanceof Address)
const user2 = new User({name: 'Bob'})
console.log(user2)
console.log('user2.address instanceof Address:', user2.address instanceof Address) |
Feel free to submit a PR if you have something working. |
Should this be rebased or closed? I'd love to see the definition for Record's constructor (and the class it returns) improved in flow. |
I'd say closed and rewritten |
@ccorcos, from conversation above, it looks like this PR should be closed. Let me know if it is not correct assumption. 😅 |
Thanks for responding and tagging this issue @kozlitinaelja! It would probably help the community a lot if we could get the flow definitions that are shipped with this package moved out into |
@kozlitinaelja this PR is based off of #961 which simply defines types for the module default exports. So in fact, its only a 2 line change adding type definitions to The other PR you mention does not fix these PRs and only tests the existing functionality. For example Record is a TODO: https://github.com/facebook/immutable-js/pull/878/files#diff-167148588528b5fa451dc535e9c29037R429 |
@ccorcos could you remove the unrelated changes of handling exports? We should tackle that in a different PR. Also, please add some tests (see type-definitions/tests) that describe the correct behavior. |
@wokalski unfortunately, mystifying Flow errors occasionally occur when I use my The other problem with turning my suggestion into a PR is it's so verbose to use and I'm not sure I can make it work for both class extension and non-class extension usage of |
@jedwards1211 I'd say let's wait for the conclusion of this PR. @rgbkrk I started doing it in a PR to |
Closed by #1073 |
#1073 seems to be related to TS. Records are still missing most of the definitions in flow:) |
Yeah my mistake on typescript vs flow specific. I'll get equivalent changes out to flow types next :) |
Lovely. :) Just in case its needed and you are not aware of it already, there is this gist on the subject: |
I added
update
andwithMutations
. The definitions forsetIn
,getIn
, andupdateIn
are going to be a bit trickier.Looking here for functions not included in the documentations: https://github.com/facebook/immutable-js/blob/master/src/Record.js#L131-L146
FYI this PR contains a commit from https://github.com/facebook/immutable-js/blob/master/dist/immutable.js.flow