8000 Overhaul flow type definitions for Record by leebyron · Pull Request #1104 · immutable-js/immutable-js · GitHub
[go: up one dir, main page]

Skip to content

Overhaul flow type definitions for Record #1104

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

Merged
merged 1 commit into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/immutable-nonambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@
*
* @alias toJSON
*/
toJS(): any;
toJS(): Object;

/**
* Shallowly converts this Record to equivalent JS.
Expand Down
2 changes: 1 addition & 1 deletion dist/immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ declare module Immutable {
*
* @alias toJSON
*/
toJS(): any;
toJS(): Object;

/**
* Shallowly converts this Record to equivalent JS.
Expand Down
50 changes: 42 additions & 8 deletions dist/immutable.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -1033,16 +1033,50 @@ declare class Stack<+T> extends IndexedCollection<T> {
declare function Range(start?: number, end?: number, step?: number): IndexedSeq<number>;
declare function Repeat<T>(value: T, times?: number): IndexedSeq<T>;

// TODO: Once flow can extend normal Objects we can change this back to actually reflect Record behavior.
// For now fallback to any to not break existing code
declare class Record<T: Object> {
static <T: Object>(spec: T, name?: string): /*T & Record<T>*/any;
declare class Record {
static <Values>(spec: Values, name?: string): RecordClass<Values>;
constructor<Values>(spec: Values, name?: string): RecordClass<Values>;

static getDescriptiveName(record: Record<any>): string;
static getDescriptiveName(record: RecordInstance<*>): string;
}

declare interface RecordClass<T> {
(values: $Shape<T> | ESIterable<[string, any]>): RecordInstance<T> & T;
new (values: $Shape<T> | ESIterable<[string, any]>): RecordInstance<T> & T;
}

declare class RecordInstance<T: Object> {
size: number;

has(key: string): boolean;
get<K: $Keys<T>>(key: K): /*T[K]*/any;

equals(other: any): boolean;
hashCode(): number;

set<K: $Keys<T>>(key: K, value: /*T[K]*/any): this;
update<K: $Keys<T>>(key: K, updater: (value: /*T[K]*/any) => /*T[K]*/any): this;
merge(...iterables: Array<$Shape<T> | ESIterable<[string, any]>>): this;
mergeDeep(...iterables: Array<$Shape<T> | ESIterable<[string, any]>>): this;

delete(key: $Keys<T>): this;
remove(key: $Keys<T>): this;
clear(): this;

setIn(keyPath: ESIterable<any>, value: any): this;
updateIn(keyPath: ESIterable<any>, updater: (value: any) => any): this;
mergeIn(keyPath: ESIterable<any>, ...iterables: Array<any>): this;
mergeDeepIn(keyPath: ESIterable<any>, ...iterables: Array<any>): this;
deleteIn(keyPath: ESIterable<any>): this;
removeIn(keyPath: ESIterable<any>): this;

toJS(): T;
toObject(): T;
withMutations(mutator: (mutable: this) => this | void): this;
asMutable(): this;
asImmutable(): this;

get<A>(key: $Keys<T>): A;
set<A>(key: $Keys<T>, value: A): /*T & Record<T>*/this;
remove(key: $Keys<T>): /*T & Record<T>*/this;
@@iterator(): Iterator<[$Keys<T>, any]>;
}

declare function fromJS(json: mixed, reviver?: (k: any, v: Iterable<any, any>) => any): any;
Expand Down
2 changes: 1 addition & 1 deletion type-definitions/Immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ declare module Immutable {
*
* @alias toJSON
*/
toJS(): any;
toJS(): Object;

/**
* Shallowly converts this Record to equivalent JS.
Expand Down
50 changes: 42 additions & 8 deletions type-definitions/immutable.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -1033,16 +1033,50 @@ declare class Stack<+T> extends IndexedCollection<T> {
declare function Range(start?: number, end?: number, step?: number): IndexedSeq<number>;
declare function Repeat<T>(value: T, times?: number): IndexedSeq<T>;

// TODO: Once flow can extend normal Objects we can change this back to actually reflect Record behavior.
// For now fallback to any to not break existing code
declare class Record<T: Object> {
static <T: Object>(spec: T, name?: string): /*T & Record<T>*/any;
declare class Record {
static <Values>(spec: Values, name?: string): RecordClass<Values>;
constructor<Values>(spec: Values, name?: string): RecordClass<Values>;

static getDescriptiveName(record: Record<any>): string;
static getDescriptiveName(record: RecordInstance<*>): string;
}

declare interface RecordClass<T> {
(values: $Shape<T> | ESIterable<[string, any]>): RecordInstance<T> & T;
new (values: $Shape<T> | ESIterable<[string, any]>): RecordInstance<T> & T;
}

declare class RecordInstance<T: Object> {
size: number;

has(key: string): boolean;
get<K: $Keys<T>>(key: K): /*T[K]*/any;

equals(other: any): boolean;
hashCode(): number;

set<K: $Keys<T>>(key: K, value: /*T[K]*/any): this;
update<K: $Keys<T>>(key: K, updater: (value: /*T[K]*/any) => /*T[K]*/any): this;
merge(...iterables: Array<$Shape<T> | ESIterable<[string, any]>>): this;
mergeDeep(...iterables: Array<$Shape<T> | ESIterable<[string, any]>>): this;

delete(key: $Keys<T>): this;
remove(key: $Keys<T>): this;
clear(): this;

setIn(keyPath: ESIterable<any>, value: any): this;
updateIn(keyPath: ESIterable<any>, updater: (value: any) => any): this;
mergeIn(keyPath: ESIterable<any>, ...iterables: Array<any>): this;
mergeDeepIn(keyPath: ESIterable<any>, ...iterables: Array<any>): this;
deleteIn(keyPath: ESIterable<any>): this;
removeIn(keyPath: ESIterable<any>): this;

toJS(): T;
toObject(): T;
withMutations(mutator: (mutable: this) => this | void): this;
asMutable(): this;
asImmutable(): this;

get<A>(key: $Keys<T>): A;
set<A>(key: $Keys<T>, value: A): /*T & Record<T>*/this;
remove(key: $Keys<T>): /*T & Record<T>*/this;
@@iterator(): Iterator<[$Keys<T>, any]>;
}

declare function fromJS(json: mixed, reviver?: (k: any, v: Iterable<any, any>) => any): any;
Expand Down
3 changes: 0 additions & 3 deletions type-definitions/tests/immutable-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,3 @@ numberStack = Stack(['a']).flatten()
{ const stringSequence: IndexedSeq<string> = Repeat(0, 1) }
// $ExpectError
{ const stringSequence: IndexedSeq<string> = Range(0, 0, 0) }

/* Record */
// TODO
60 changes: 60 additions & 0 deletions type-definitions/tests/record.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* @flow
*/

// Some tests look like they are repeated in order to avoid false positives.
// Flow might not complain about an instance of (what it thinks is) T to be assigned to T<K, V>

import { Record } from '../../';

const Point2 = Record({x:0, y:0});
const Point3 = Record({x:0, y:0, z:0});
const GeoPoint = Record({lat:(null: ?number), lon:(null: ?number)});

let origin2 = Point2({});
let origin3 = Point3({});
let geo = GeoPoint({lat:34});
// $ExpectError
const mistake = Point2({x:'string'});
origin3 = GeoPoint({lat:34})
geo = Point3({});

const px = origin2.get('x');
const px2 = origin2.x;
// $ExpectError
const pz = origin2.get('z');
// $ExpectError
const pz2 = origin2.z;

origin2.set('x', 4);
// Note: this should be an error, but Flow does not yet support index types.
origin2.set('x', 'string');
// $ExpectError
origin2.set('z', 3);

const name: string = Record.getDescriptiveName(origin2);
// $ExpectError
const name: string = Record.getDescriptiveName({});

// Note: need to cast through any when extending Records as if they ere classes
class ABClass extends (Record({a:1, b:2}): any) {
setA(a: number) {
return this.set('a', a);
}

setB(b: number) {
return this.set('b', b);
}
}

var t1 = new ABClass({a: 1});
var t2 = t1.setA(3);
var t3 = t2.setB(10);
// Note: flow does not check extended Record classes yet
var t4 = t2.setC(10);

var t1a = t1.a;
// Note: flow does not check extended Record classes yet
var t1a: string = t1.a;
// Note: flow does not check extended Record classes yet
var t1c = t1.c;
0