Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Flow: Record: Fix created class types #1193

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

Closed
wants to merge 6 commits into from
Closed

Flow: Record: Fix created class types #1193

wants to merge 6 commits into from

Conversation

oreqizer
Copy link

Change definition for Record where constructor was specified with 'new'.

Change definition for Record where constructor was specified with 'new'.
@oreqizer oreqizer changed the title fix(flow): Change 'new' to 'constructor' WIP: Flow: Record: Change 'new' to 'constructor' Mar 30, 2017
Makes Record return a Class type instead of an interface.
Fixes an issue with constructor calls.
There was an in-between step when constructing Records.
Records now directly return a RecordClass that's the former
RecordInstance.
It was overriding RecordClass' generic signature.
@oreqizer oreqizer changed the title WIP: Flow: Record: Change 'new' to 'constructor' Flow: Record: Change 'new' to 'constructor' Mar 30, 2017
@oreqizer oreqizer changed the title Flow: Record: Change 'new' to 'constructor' Flow: Record: Fix created class types Mar 30, 2017
@ianwcarlson
Copy link

@oreqizer This solution works a lot better for me. The downside is it requires making a dummy instance of RecordClass for typeof to work. Example:

import { Record } from 'immutable';

const testRecord = Record({
  foo: '',
  bar: 0,
});

const dummyInstance = testRecord(); // can't use "new" here
const anotherInstance = testRecord();

// can't use typeof testRecord here
const testFunc = (inputRecord: typeof dummyInstance) => null;

testFunc(anotherInstance);

Semantically it makes sense that a record class is different than an instance of it, but it makes the code a little awkward. Still, better than before where I had to create my own intersection type from RecordInstance to get this to work.

I've been trying to come up with a clean solution myself, but haven't so far.

@gajus
Copy link

gajus commented May 25, 2017

Whats stopping this from being released?

It looks like a reasonable approach.

@kozlitinaelja
Copy link
Contributor

@leebyron, are you agree with this approach? =)

@leebyron
Copy link
Collaborator

new is generally discouraged across all of Immutable.js - these are factory functions (like String or Number) and not class constructors. We only use class in flow types where necessary to help flow understand type refinements it otherwise wouldn't understand.

One unfortunate issue with this approach is that while it technically allows you to use new, the resulting value is only a class instance and does not also have property types.

That might be fine, but it's a limitation.

I'm going to amend this PR to also include a RecordOf higher-order type which allows you to better type the return of a record factory function call.

leebyron pushed a commit that referenced this pull request Sep 30, 2017
Squashed commit of the following:

commit 1d9f98adc72f40754c6a8a10e68a21388254302e
Author: Travis CI <[email protected]>
Date:   Fri Sep 29 17:37:08 2017 -0700

    Rename RecordClass to RecordInstance and include RecordOf

commit a5c82d59d5b172e658cf1dcf099e0fe601910f36
Merge: cf17600 1f66a82
Author: Travis CI <[email protected]>
Date:   Fri Sep 29 16:26:48 2017 -0700

    Merge branch 'master' of https://github.com/oreqizer/immutable-js into oreqizer-master

commit 1f66a82
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:13:18 2017 +0200

    feat(flow): Add more tests for Record constructor

commit f0addf8
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:11:11 2017 +0200

    feat(flow): Add test for Record constructor

commit 54d4025
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:10:37 2017 +0200

    fix(flow): Remove <T> in constructors

    It was overriding RecordClass' generic signature.

commit 94a54a0
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:02:26 2017 +0200

    refactor(flow): Directly return RecordClass

    There was an in-between step when constructing Records.
    Records now directly return a RecordClass that's the former
    RecordInstance.

commit ec1a4a6
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 18:31:00 2017 +0200

    fix(flow): Record returns a Class<T>

    Makes Record return a Class type instead of an interface.
    Fixes an issue with constructor calls.

commit 51c0e97
Author: Boris <[email protected]>
Date:   Thu Mar 30 17:38:03 2017 +0200

    fix(flow): Change 'new' to 'constructor'

    Change definition for Record where constructor was specified with 'new'.
@leebyron
Copy link
Collaborator

Specifically check out https://github.com/facebook/immutable-js/blob/master/type-definitions/tests/record.js#L66-L83 for how this ends up affecting the ability to type Records

@leebyron leebyron closed this Sep 30, 2017
leebyron pushed a commit that referenced this pull request Sep 30, 2017
Squashed commit of the following:

commit 1d9f98adc72f40754c6a8a10e68a21388254302e
Author: Travis CI <[email protected]>
Date:   Fri Sep 29 17:37:08 2017 -0700

    Rename RecordClass to RecordInstance and include RecordOf

commit a5c82d59d5b172e658cf1dcf099e0fe601910f36
Merge: cf17600 1f66a82
Author: Travis CI <[email protected]>
Date:   Fri Sep 29 16:26:48 2017 -0700

    Merge branch 'master' of https://github.com/oreqizer/immutable-js into oreqizer-master

commit 1f66a82
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:13:18 2017 +0200

    feat(flow): Add more tests for Record constructor

commit f0addf8
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:11:11 2017 +0200

    feat(flow): Add test for Record constructor

commit 54d4025
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:10:37 2017 +0200

    fix(flow): Remove <T> in constructors

    It was overriding RecordClass' generic signature.

commit 94a54a0
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:02:26 2017 +0200

    refactor(flow): Directly return RecordClass

    There was an in-between step when constructing Records.
    Records now directly return a RecordClass that's the former
    RecordInstance.

commit ec1a4a6
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 18:31:00 2017 +0200

    fix(flow): Record returns a Class<T>

    Makes Record return a Class type instead of an interface.
    Fixes an issue with constructor calls.

commit 51c0e97
Author: Boris <[email protected]>
Date:   Thu Mar 30 17:38:03 2017 +0200

    fix(flow): Change 'new' to 'constructor'

    Change definition for Record where constructor was specified with 'new'.
leebyron pushed a commit that referenced this pull request Sep 30, 2017
Squashed commit of the following:

commit 1d9f98adc72f40754c6a8a10e68a21388254302e
Author: Travis CI <[email protected]>
Date:   Fri Sep 29 17:37:08 2017 -0700

    Rename RecordClass to RecordInstance and include RecordOf

commit a5c82d59d5b172e658cf1dcf099e0fe601910f36
Merge: cf17600 1f66a82
Author: Travis CI <[email protected]>
Date:   Fri Sep 29 16:26:48 2017 -0700

    Merge branch 'master' of https://github.com/oreqizer/immutable-js into oreqizer-master

commit 1f66a82
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:13:18 2017 +0200

    feat(flow): Add more tests for Record constructor

commit f0addf8
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:11:11 2017 +0200

    feat(flow): Add test for Record constructor

commit 54d4025
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:10:37 2017 +0200

    fix(flow): Remove <T> in constructors

    It was overriding RecordClass' generic signature.

commit 94a54a0
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 19:02:26 2017 +0200

    refactor(flow): Directly return RecordClass

    There was an in-between step when constructing Records.
    Records now directly return a RecordClass that's the former
    RecordInstance.

commit ec1a4a6
Author: oreqizer <[email protected]>
Date:   Thu Mar 30 18:31:00 2017 +0200

    fix(flow): Record returns a Class<T>

    Makes Record return a Class type instead of an interface.
    Fixes an issue with constructor calls.

commit 51c0e97
Author: Boris <[email protected]>
Date:   Thu Mar 30 17:38:03 2017 +0200

    fix(flow): Change 'new' to 'constructor'

    Change definition for Record where constructor was specified with 'new'.
errendir added a commit to errendir/immutable-js that referenced this pull request Sep 30, 2017
…ings

* facebook/master: (48 commits)
  4.0.0-rc.3
  Use latest immutable.js build on website
  Only build docs on tagged releases (immutable-js#1321)
  Relicense as MIT (immutable-js#1320)
  Fix rendering issue on tall screens
  Merge immutable-js#1285
  Do not throw from hasIn (immutable-js#1319)
  Add more flow tests for Records
  Upgrade prettier and jest (immutable-js#1318)
  Merge immutable-js#1193
  Minor flow record fixes
  Merges immutable-js#1195
  fixed immutable-js#1290 by removing relative position from star count (immutable-js#1317)
  Fix deploy script
  Adds a script which automatically builds a #npm branch (immutable-js#1316)
  Use .github directory
  Merges immutable-js#1314
  Flow config ignore all node_modules
  Update to latest version of flow (immutable-js#1312)
  fixed immutable-js#1313 by fixing two borked reducers (immutable-js#1315)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants