Skip to content

Commit ed97bc8

Browse files
committed
Add type testing
1 parent d993755 commit ed97bc8

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ jobs:
3232
- name: build
3333
run: npm run build --if-present
3434
- name: Vitest
35-
run: npx vitest
35+
run: npx vitest --typecheck

index.test-d.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {expectTypeOf, it} from 'vitest';
2+
import ManyKeysMap from './index.js';
3+
4+
it('should be a class that extends Map', () => {
5+
// Use toBeConstructibleWith() for checking if a class is constructible
6+
expectTypeOf(ManyKeysMap).toBeConstructibleWith();
7+
expectTypeOf(ManyKeysMap.prototype).toMatchTypeOf(Map.prototype);
8+
});
9+
10+
it('should correctly infer key and value types', () => {
11+
type KeyTuple = [string, number];
12+
type ValueType = {id: string; name: string};
13+
14+
const map = new ManyKeysMap<KeyTuple, ValueType>();
15+
16+
// Use toEqualTypeOf() for checking the type of an instance
17+
expectTypeOf(map).toEqualTypeOf<ManyKeysMap<KeyTuple, ValueType>>();
18+
expectTypeOf(map).not.toEqualTypeOf<ManyKeysMap<[number], string>>();
19+
20+
// Test set method
21+
expectTypeOf(map.set).parameter(0).toEqualTypeOf<KeyTuple>();
22+
expectTypeOf(map.set).parameter(1).toEqualTypeOf<ValueType>();
23+
expectTypeOf(map.set(['a', 1], {id: 'x', name: 'y'})).toEqualTypeOf<typeof map>();
24+
25+
// Test get method
26+
expectTypeOf(map.get).parameter(0).toEqualTypeOf<KeyTuple>();
27+
expectTypeOf(map.get(['a', 1])).toEqualTypeOf<ValueType | undefined>();
28+
29+
// Test has method
30+
expectTypeOf(map.has).parameter(0).toEqualTypeOf<KeyTuple>();
31+
expectTypeOf(map.has(['a', 1])).toEqualTypeOf<boolean>();
32+
33+
// Test delete method
34+
expectTypeOf(map.delete).parameter(0).toEqualTypeOf<KeyTuple>();
35+
expectTypeOf(map.delete(['a', 1])).toEqualTypeOf<boolean>();
36+
});
37+
38+
it('should handle different key and value types', () => {
39+
type DiverseKey = [boolean, number, string];
40+
type DiverseValue = {status: 'active' | 'inactive'};
41+
42+
const map = new ManyKeysMap<DiverseKey, DiverseValue>();
43+
44+
expectTypeOf(map.set([true, 123, 'hello'], {status: 'active'})).toEqualTypeOf<typeof map>();
45+
expectTypeOf(map.get([false, 456, 'world'])).toEqualTypeOf<DiverseValue | undefined>();
46+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"index.js"
2525
],
2626
"scripts": {
27-
"test": "tsc && xo && vitest"
27+
"test": "tsc && xo && vitest --typecheck"
2828
},
2929
"xo": {
3030
"rules": {

0 commit comments

Comments
 (0)