Skip to content

Commit

Permalink
chore: migrate type tests to TSTyche
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed Feb 21, 2024
1 parent ac05955 commit 94ed56e
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 177 deletions.
7 changes: 5 additions & 2 deletions .prettierignore
@@ -1,5 +1,8 @@
dist
pages/generated
pages/out
type-definitions/ts-tests/
type-definitions/flow-tests/
type-definitions/ts-tests/*.ts
type-definitions/flow-tests/

!type-definitions/ts-tests/covariance.ts
!type-definitions/ts-tests/deepCopy.ts
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -55,7 +55,9 @@
"npm": ">=7.0.0"
},
"scripts": {
"test": "run-s format lint type-check build unit-test",
"test": "run-s format lint type-check build test:*",
"test:unit": "jest",
"test:types": "tstyche",
"format": "npm run lint:format -- --write",
"lint": "run-s lint:*",
"lint:format": "prettier --check \"{__tests__,src,type-definitions,website/src,perf,resources}/**/*{.js,.ts,.tsx,.flow,.css}\"",
Expand All @@ -69,7 +71,6 @@
"build:types": "cpy ./type-definitions/immutable.* dist",
"build:prepare": "./resources/prepare-dist.sh",
"build:stats": "node ./resources/dist-stats.mjs",
"unit-test": "jest",
"website:build": "cd website && next build && next-sitemap",
"website:dev": "cd website && next dev",
"check-git-clean": "./resources/check-git-clean.sh",
Expand Down Expand Up @@ -127,6 +128,7 @@
"rollup": "3.28.1",
"size-limit": "^8.2.6",
"transducers-js": "0.4.174",
"tstyche": "^1.0.0",
"typescript": "5.1"
},
"size-limit": [
Expand Down
8 changes: 8 additions & 0 deletions tstyche.config.json
@@ -0,0 +1,8 @@
{
"$schema": "https://tstyche.org/schemas/config.json",
"target": ["4.5", "5.0", "current"],
"testFileMatch": [
"**/type-definitions/ts-tests/covariance.ts",
"**/type-definitions/ts-tests/deepCopy.ts"
]
}
114 changes: 61 additions & 53 deletions type-definitions/ts-tests/covariance.ts
@@ -1,4 +1,13 @@
import { List, Map, OrderedMap, OrderedSet, Set, Stack } from 'immutable';
import { expect, test } from 'tstyche';
import {
List,
Map,
MapOf,
OrderedMap,
OrderedSet,
Set,
Stack,
} from 'immutable';

class A {
x: number;
Expand All @@ -7,6 +16,7 @@ class A {
this.x = 1;
}
}

class B extends A {
y: string;

Expand All @@ -15,6 +25,7 @@ class B extends A {
this.y = 'B';
}
}

class C {
z: string;

Expand All @@ -23,55 +34,52 @@ class C {
}
}

// List covariance
let listOfB: List<B> = List<B>();
let listOfA: List<A> = listOfB;

// $ExpectType List<B>
listOfA = List([new B()]);

// $ExpectError
let listOfC: List<C> = listOfB;

// Map covariance
declare let mapOfB: Map<string, B>;
let mapOfA: Map<string, A> = mapOfB;

// $ExpectType MapOf<{ b: B; }>
mapOfA = Map({ b: new B() });

// $ExpectError
let mapOfC: Map<string, C> = mapOfB;

// Set covariance
declare let setOfB: Set<B>;
let setOfA: Set<A> = setOfB;

// $ExpectType Set<B>
setOfA = Set([new B()]);
// $ExpectError
let setOfC: Set<C> = setOfB;

// Stack covariance
declare let stackOfB: Stack<B>;
let stackOfA: Stack<A> = stackOfB;
// $ExpectType Stack<B>
stackOfA = Stack([new B()]);
// $ExpectError
let stackOfC: Stack<C> = stackOfB;

// OrderedMap covariance
declare let orderedMapOfB: OrderedMap<string, B>;
let orderedMapOfA: OrderedMap<string, A> = orderedMapOfB;
// $ExpectType OrderedMap<string, B>
orderedMapOfA = OrderedMap({ b: new B() });
// $ExpectError
let orderedMapOfC: OrderedMap<string, C> = orderedMapOfB;

// OrderedSet covariance
declare let orderedSetOfB: OrderedSet<B>;
let orderedSetOfA: OrderedSet<A> = orderedSetOfB;
// $ExpectType OrderedSet<B>
orderedSetOfA = OrderedSet([new B()]);
// $ExpectError
let orderedSetOfC: OrderedSet<C> = orderedSetOfB;
test('List covariance', () => {
expect<List<A>>().type.toBeAssignable(List<B>());

expect(List([new B()])).type.toEqual<List<B>>();

expect<List<C>>().type.not.toBeAssignable(List<B>());
});

test('Map covariance', () => {
expect<Map<string, A>>().type.toBeAssignable<Map<string, B>>();

expect(Map({ b: new B() })).type.toEqual<MapOf<{ b: B }>>();

expect<Map<string, C>>().type.not.toBeAssignable<Map<string, B>>();
});

test('Set covariance', () => {
expect<Set<A>>().type.toBeAssignable<Set<B>>();

expect(Set([new B()])).type.toEqual<Set<B>>();

expect<Set<C>>().type.not.toBeAssignable<Set<B>>();
});

test('Stack covariance', () => {
expect<Stack<A>>().type.toBeAssignable<Stack<B>>();

expect(Stack([new B()])).type.toEqual<Stack<B>>();

expect<Stack<C>>().type.not.toBeAssignable<Stack<B>>();
});

test('OrderedMap covariance', () => {
expect<OrderedMap<string, A>>().type.toBeAssignable<OrderedMap<string, B>>();

expect(OrderedMap({ b: new B() })).type.toEqual<OrderedMap<string, B>>();

expect<OrderedMap<string, C>>().type.not.toBeAssignable<
OrderedMap<string, B>
>();
});

test('OrderedSet covariance', () => {
expect<OrderedSet<A>>().type.toBeAssignable<OrderedSet<B>>();

expect(OrderedSet([new B()])).type.toEqual<OrderedSet<B>>();

expect<OrderedSet<C>>().type.not.toBeAssignable<OrderedSet<B>>();
});

0 comments on commit 94ed56e

Please sign in to comment.