diff --git a/.prettierignore b/.prettierignore index 5ee00f8c0..598d2e35c 100644 --- a/.prettierignore +++ b/.prettierignore @@ -8,3 +8,7 @@ type-definitions/flow-tests/ !type-definitions/ts-tests/deepCopy.ts !type-definitions/ts-tests/empty.ts !type-definitions/ts-tests/es6-collections.ts +!type-definitions/ts-tests/exports.ts +!type-definitions/ts-tests/from-js.ts +!type-definitions/ts-tests/functional.ts +!type-definitions/ts-tests/groupBy.ts diff --git a/tstyche.config.json b/tstyche.config.json index 22b2d9784..8ba7a2a9d 100644 --- a/tstyche.config.json +++ b/tstyche.config.json @@ -4,6 +4,10 @@ "**/type-definitions/ts-tests/covariance.ts", "**/type-definitions/ts-tests/deepCopy.ts", "**/type-definitions/ts-tests/empty.ts", - "**/type-definitions/ts-tests/es6-collections.ts" + "**/type-definitions/ts-tests/es6-collections.ts", + "**/type-definitions/ts-tests/exports.ts", + "**/type-definitions/ts-tests/from-js.ts", + "**/type-definitions/ts-tests/functional.ts", + "**/type-definitions/ts-tests/groupBy.ts" ] } diff --git a/type-definitions/ts-tests/exports.ts b/type-definitions/ts-tests/exports.ts index 4716473c2..8c0f08b08 100644 --- a/type-definitions/ts-tests/exports.ts +++ b/type-definitions/ts-tests/exports.ts @@ -1,5 +1,6 @@ // Some tests look like they are repeated in order to avoid false positives. +import { expect, test } from 'tstyche'; import * as Immutable from 'immutable'; import { List, @@ -14,32 +15,98 @@ import { Collection, } from 'immutable'; -List; // $ExpectType typeof List -Map; // $ExpectType typeof Map -OrderedMap; // $ExpectType typeof OrderedMap -OrderedSet; // $ExpectType typeof OrderedSet -// TODO: Turn on once https://github.com/Microsoft/dtslint/issues/19 is resolved. -Range; // $ ExpectType (start?: number | undefined, end?: number | undefined, step?: number | undefined) => Indexed -Repeat; // $ExpectType (value: T, times?: number | undefined) => Indexed -Seq; // $ExpectType typeof Seq -Set; // $ExpectType typeof Set -Stack; // $ExpectType typeof Stack -Collection; // $ExpectType typeof Collection -Collection.Set; // $ExpectType (collection?: Iterable | ArrayLike | undefined) => Set -Collection.Keyed; // $ ExpectType { (collection: Iterable<[K, V]>): Keyed; (obj: { [key: string]: V; }): Keyed } -Collection.Indexed; // $ExpectType (collection?: Iterable | ArrayLike | undefined) => Indexed - -Immutable.List; // $ExpectType typeof List -Immutable.Map; // $ExpectType typeof Map -Immutable.OrderedMap; // $ExpectType typeof OrderedMap -Immutable.OrderedSet; // $ExpectType typeof OrderedSet -// TODO: Turn on once https://github.com/Microsoft/dtslint/issues/19 is resolved. -Immutable.Range; // $ ExpectType (start?: number | undefined, end?: number | undefined, step?: number | undefined) => Indexed -Immutable.Repeat; // $ExpectType (value: T, times?: number | undefined) => Indexed -Immutable.Seq; // $ExpectType typeof Seq -Immutable.Set; // $ExpectType typeof Set -Immutable.Stack; // $ExpectType typeof Stack -Immutable.Collection; // $ExpectType typeof Collection -Immutable.Collection.Set; // $ExpectType (collection?: Iterable | ArrayLike | undefined) => Set -Immutable.Collection.Keyed; // $ ExpectType { (collection: Iterable<[K, V]>): Keyed; (obj: { [key: string]: V; }): Keyed } -Immutable.Collection.Indexed; // $ExpectType (collection?: Iterable | ArrayLike | undefined) => Indexed +test('named imports', () => { + expect(List).type.toEqual(); + + expect(Map).type.toEqual(); + + expect(OrderedMap).type.toEqual(); + + expect(OrderedSet).type.toEqual(); + + expect(Range).type.toEqual< + ( + start: number, + end: number, + step?: number | undefined + ) => Seq.Indexed + >(); + + expect(Repeat).type.toEqual< + (value: T, times?: number | undefined) => Seq.Indexed + >(); + + expect(Seq).type.toEqual(); + + expect(Set).type.toEqual(); + + expect(Stack).type.toEqual(); + + expect(Collection).type.toEqual(); + + expect(Collection.Set).type.toEqual< + ( + collection?: Iterable | ArrayLike | undefined + ) => Collection.Set + >(); + + expect(Collection.Keyed).type.toEqual<{ + (collection?: Iterable<[K, V]> | undefined): Collection.Keyed; + (obj: { [key: string]: V }): Collection.Keyed; + }>(); + + expect(Collection.Indexed).type.toEqual< + ( + collection?: Iterable | ArrayLike | undefined + ) => Collection.Indexed + >(); +}); + +test('namespace import', () => { + expect(Immutable.List).type.toEqual(); + + expect(Immutable.Map).type.toEqual(); + + expect(Immutable.OrderedMap).type.toEqual(); + + expect(Immutable.OrderedSet).type.toEqual(); + + expect(Immutable.Range).type.toEqual< + ( + start: number, + end: number, + step?: number | undefined + ) => Immutable.Seq.Indexed + >(); + + expect(Immutable.Repeat).type.toEqual< + (value: T, times?: number | undefined) => Immutable.Seq.Indexed + >(); + + expect(Immutable.Seq).type.toEqual(); + + expect(Immutable.Set).type.toEqual(); + + expect(Immutable.Stack).type.toEqual(); + + expect(Immutable.Collection).type.toEqual(); + + expect(Immutable.Collection.Set).type.toEqual< + ( + collection?: Iterable | ArrayLike | undefined + ) => Immutable.Collection.Set + >(); + + expect(Immutable.Collection.Keyed).type.toEqual<{ + ( + collection?: Iterable<[K, V]> | undefined + ): Immutable.Collection.Keyed; + (obj: { [key: string]: V }): Immutable.Collection.Keyed; + }>(); + + expect(Immutable.Collection.Indexed).type.toEqual< + ( + collection?: Iterable | ArrayLike | undefined + ) => Immutable.Collection.Indexed + >(); +}); diff --git a/type-definitions/ts-tests/from-js.ts b/type-definitions/ts-tests/from-js.ts index 994ee16fd..14fb84194 100644 --- a/type-definitions/ts-tests/from-js.ts +++ b/type-definitions/ts-tests/from-js.ts @@ -1,49 +1,42 @@ -import { fromJS, List, Map } from 'immutable'; +import { expect, test } from 'tstyche'; +import { fromJS, Collection, List, Map, MapOf } from 'immutable'; -{ - // fromJS +test('fromJS', () => { + expect(fromJS({}, (a: any, b: any) => b)).type.toEqual< + Collection + >(); - // $ExpectType Collection - fromJS({}, (a: any, b: any) => b); + expect(fromJS('abc')).type.toBeString(); - // $ExpectType string - fromJS('abc'); + expect(fromJS([0, 1, 2])).type.toEqual>(); - // $ExpectType List - fromJS([0, 1, 2]); + expect(fromJS(List([0, 1, 2]))).type.toEqual>(); - // $ExpectType List - fromJS(List([0, 1, 2])); + expect(fromJS({ a: 0, b: 1, c: 2 })).type.toEqual< + Map<'b' | 'a' | 'c', number> + >(); - // $ExpectType Map<"b" | "a" | "c", number> - fromJS({a: 0, b: 1, c: 2}); + expect(fromJS(Map({ a: 0, b: 1, c: 2 }))).type.toEqual< + MapOf<{ a: number; b: number; c: number }> + >(); - // $ExpectType MapOf<{ a: number; b: number; c: number; }> - fromJS(Map({a: 0, b: 1, c: 2})); + expect(fromJS([{ a: 0 }])).type.toEqual>>(); - // $ExpectType List> - fromJS([{a: 0}]); + expect(fromJS({ a: [0] })).type.toEqual>>(); - // $ExpectType Map<"a", List> - fromJS({a: [0]}); + expect(fromJS([[[0]]])).type.toEqual>>>(); - // $ExpectType List>> - fromJS([[[0]]]); - - // $ExpectType Map<"a", Map<"b", Map<"c", number>>> - fromJS({a: {b: {c: 0}}}); -} - -{ - // fromJS in an array of function + expect(fromJS({ a: { b: { c: 0 } } })).type.toEqual< + Map<'a', Map<'b', Map<'c', number>>> + >(); +}); +test('fromJS in an array of function', () => { const create = [(data: any) => data, fromJS][1]; - // $ExpectType any - create({ a: 'A' }); + expect(create({ a: 'A' })).type.toBeAny(); const createConst = ([(data: any) => data, fromJS] as const)[1]; - // $ExpectType Map<"a", string> - createConst({ a: 'A' }); -} + expect(createConst({ a: 'A' })).type.toEqual>(); +}); diff --git a/type-definitions/ts-tests/functional.ts b/type-definitions/ts-tests/functional.ts index 1554eeab3..a2efc6a70 100644 --- a/type-definitions/ts-tests/functional.ts +++ b/type-definitions/ts-tests/functional.ts @@ -1,84 +1,63 @@ -import { - get, - has, - set, - remove, - update, -} from 'immutable'; +import { expect, test } from 'tstyche'; +import { get, has, set, remove, update } from 'immutable'; -{ - // get +test('get', () => { + expect(get([1, 2, 3], 0)).type.toEqual(); - // $ExpectType number | undefined - get([1, 2, 3], 0); + expect(get([1, 2, 3], 0, 'a')).type.toEqual(); - // $ExpectType number | "a" - get([1, 2, 3], 0, 'a'); + expect(get({ x: 10, y: 20 }, 'x')).type.toEqual(); - // $ExpectType number | undefined - get({ x: 10, y: 20 }, 'x'); + expect(get({ x: 10, y: 20 }, 'z', 'missing')).type.toEqual< + number | 'missing' + >(); +}); - // $ExpectType number | "missing" - get({ x: 10, y: 20 }, 'z', 'missing'); -} +test('has', () => { + expect(has([1, 2, 3], 0)).type.toBeBoolean(); -{ - // has + expect(has({ x: 10, y: 20 }, 'x')).type.toBeBoolean(); +}); - // $ExpectType boolean - has([1, 2, 3], 0); +test('set', () => { + expect(set([1, 2, 3], 0, 10)).type.toEqual(); - // $ExpectType boolean - has({ x: 10, y: 20 }, 'x'); -} + expect(set([1, 2, 3], 0, 'a')).type.toRaiseError(); -{ - // set + expect(set([1, 2, 3], 'a', 0)).type.toRaiseError(); - // $ExpectType number[] - set([1, 2, 3], 0, 10); + expect(set({ x: 10, y: 20 }, 'x', 100)).type.toEqual<{ + x: number; + y: number; + }>(); - // $ExpectError - set([1, 2, 3], 0, 'a'); + expect(set({ x: 10, y: 20 }, 'x', 'a')).type.toRaiseError(); +}); - // $ExpectError - set([1, 2, 3], 'a', 0); +test('remove', () => { + expect(remove([1, 2, 3], 0)).type.toEqual(); - // $ExpectType { x: number; y: number; } - set({ x: 10, y: 20 }, 'x', 100); + expect(remove({ x: 10, y: 20 }, 'x')).type.toEqual<{ + x: number; + y: number; + }>(); +}); - // $ExpectError - set({ x: 10, y: 20 }, 'x', 'a'); -} +test('update', () => { + expect(update([1, 2, 3], 0, (v: number) => v + 1)).type.toEqual(); -{ - // remove + expect(update([1, 2, 3], 0, 1)).type.toRaiseError(); - // $ExpectType number[] - remove([1, 2, 3], 0); + expect(update([1, 2, 3], 0, (v: string) => v + 'a')).type.toRaiseError(); - // $ExpectType { x: number; y: number; } - remove({ x: 10, y: 20 }, 'x'); -} + expect(update([1, 2, 3], 'a', (v: number) => v + 1)).type.toRaiseError(); -{ - // update + expect(update({ x: 10, y: 20 }, 'x', (v: number) => v + 1)).type.toEqual<{ + x: number; + y: number; + }>(); - // $ExpectType number[] - update([1, 2, 3], 0, (v: number) => v + 1); - - // $ExpectError - update([1, 2, 3], 0, 1); - - // $ExpectError - update([1, 2, 3], 0, (v: string) => v + 'a'); - - // $ExpectError - update([1, 2, 3], 'a', (v: number) => v + 1); - - // $ExpectType { x: number; y: number; } - update({ x: 10, y: 20 }, 'x', (v: number) => v + 1); - - // $ExpectError - update({ x: 10, y: 20 }, 'x', (v: string) => v + 'a'); -} + expect( + update({ x: 10, y: 20 }, 'x', (v: string) => v + 'a') + ).type.toRaiseError(); +}); diff --git a/type-definitions/ts-tests/groupBy.ts b/type-definitions/ts-tests/groupBy.ts index 2403080d6..9f1147f88 100644 --- a/type-definitions/ts-tests/groupBy.ts +++ b/type-definitions/ts-tests/groupBy.ts @@ -1,37 +1,59 @@ -import { List, Map, OrderedMap, Record, Set, Seq, Stack, OrderedSet, DeepCopy, Collection } from 'immutable'; - -{ - // $ExpectType Map> - Collection(['a', 'b', 'c', 'a']).groupBy(v => v); - - // $ExpectType Map> - Collection({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`); - - // $ExpectType Map> - List(['a', 'b', 'c', 'a']).groupBy(v => v); - - // $ExpectType Map> - Seq(['a', 'b', 'c', 'a']).groupBy(v => v); - - // $ExpectType Map> - Seq({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`); - - // $ExpectType Map> - Set(['a', 'b', 'c', 'a']).groupBy(v => v); - - // $ExpectType Map> - Stack(['a', 'b', 'c', 'a']).groupBy(v => v); - - // $ExpectType Map> - OrderedSet(['a', 'b', 'c', 'a']).groupBy(v => v); - - // $ExpectType Map> - Map({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`); - - // type should be something like Map>> but groupBy returns a wrong type with `this` - // $ExpectType Map> - Map({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`); - - // $ExpectType Map> - OrderedMap({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`); -} +import { expect, test } from 'tstyche'; +import { + Collection, + List, + Map, + OrderedMap, + Set, + OrderedSet, + Seq, + Stack, + MapOf, +} from 'immutable'; + +test('groupBy', () => { + expect(Collection(['a', 'b', 'c', 'a']).groupBy(v => v)).type.toEqual< + Map> + >(); + + expect( + Collection({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`) + ).type.toEqual>>(); + + expect(List(['a', 'b', 'c', 'a']).groupBy(v => v)).type.toEqual< + Map> + >(); + + expect(Seq(['a', 'b', 'c', 'a']).groupBy(v => v)).type.toEqual< + Map> + >(); + + expect(Seq({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`)).type.toEqual< + Map> + >(); + + expect(Set(['a', 'b', 'c', 'a']).groupBy(v => v)).type.toEqual< + Map> + >(); + + expect(Stack(['a', 'b', 'c', 'a']).groupBy(v => v)).type.toEqual< + Map> + >(); + + expect(OrderedSet(['a', 'b', 'c', 'a']).groupBy(v => v)).type.toEqual< + Map> + >(); + + expect( + Map({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`) + ).type.toEqual>>(); + + // type should be something like Map>> but groupBy returns a wrong type with `this` + expect(Map({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`)).type.toEqual< + Map> + >(); + + expect( + OrderedMap({ a: 1, b: 2, c: 3, d: 1 }).groupBy(v => `key-${v}`) + ).type.toEqual>>(); +}); diff --git a/type-definitions/ts-tests/tsconfig.json b/type-definitions/ts-tests/tsconfig.json index ecd996b49..12fa9cfb7 100644 --- a/type-definitions/ts-tests/tsconfig.json +++ b/type-definitions/ts-tests/tsconfig.json @@ -6,12 +6,10 @@ "strict": true, "types": [], "noEmit": true, - "lib": [ - "es2015" - ], + "lib": ["es2015"], "baseUrl": "./", "paths": { - "immutable": ["../../type-definitions/immutable.d.ts"] + "immutable": ["../immutable.d.ts"] } }, "exclude": ["node_modules"] diff --git a/type-definitions/ts-tests/tslint.json b/type-definitions/ts-tests/tslint.json index c8ae42edc..84b746b11 100644 --- a/type-definitions/ts-tests/tslint.json +++ b/type-definitions/ts-tests/tslint.json @@ -1,7 +1,16 @@ { "extends": "dtslint/dtslint.json", "linterOptions": { - "exclude": ["covariance.ts", "deepCopy.ts", "empty.ts", "es6-collections.ts"] + "exclude": [ + "covariance.ts", + "deepCopy.ts", + "empty.ts", + "es6-collections.ts", + "exports.ts", + "from-js.ts", + "functional.ts", + "groupBy.ts" + ] }, "rules": { "no-var": false,