Skip to content

Commit eeda51e

Browse files
authored
chore: update type tests (#27)
1 parent c5fa761 commit eeda51e

File tree

6 files changed

+35
-34
lines changed

6 files changed

+35
-34
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"test": "npm run test:unit && npm run test:types",
1212
"test:unit": "ts-mocha -n loader=ts-node/esm -p ./tsconfig.json --require ./tests/setup.ts ./tests/ut/**/*.test.ts",
1313
"test:watch": "ts-mocha -n loader=ts-node/esm -p ./tsconfig.json --require ./tests/setup.ts --watch --parallel --slow 100 ./tests/ut/**/*.test.ts",
14-
"test:types": "tstyche ./tests"
14+
"test:types": "tstyche"
1515
},
1616
"files": [
1717
"dist/**/*.js",
@@ -45,7 +45,7 @@
4545
"sinon-chai": "^4.0.1",
4646
"ts-mocha": "^11.1.0",
4747
"ts-node": "^10.9.2",
48-
"tstyche": "^4.3.0",
48+
"tstyche": "^5.0.0-beta.2",
4949
"typescript": "^5.5.4",
5050
"web-worker": "^1.5.0"
5151
},

src/misc/Queue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class Queue<TItem> {
2222
return this.#tail++;
2323
}
2424

25-
dequeue() {
25+
dequeue(): TItem {
2626
if (this.isEmpty) {
2727
throw new Error("Cannot dequeue from an empty queue.");
2828
}
@@ -31,7 +31,7 @@ export class Queue<TItem> {
3131
return item;
3232
}
3333

34-
peek() {
34+
peek(): TItem {
3535
if (this.isEmpty) {
3636
throw new Error("Cannot peek on an empty queue.");
3737
}

src/workers.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export type WorkerTasks<T extends Record<string, (...args: any) => any>> = {
2222
export type AsyncMessage<Tasks extends Record<string, (...args: any) => any>> = {
2323
task: keyof Tasks;
2424
workItemId: number;
25-
cancelToken?: Token;
26-
payload?: WorkerTasks<Tasks>[keyof Tasks]['payload'];
25+
cancelToken?: Token | undefined;
26+
payload?: WorkerTasks<Tasks>[keyof Tasks]['payload'] | undefined;
2727
};
2828

2929
/**
@@ -32,7 +32,7 @@ export type AsyncMessage<Tasks extends Record<string, (...args: any) => any>> =
3232
export type AsyncMessageUntyped = {
3333
workItemId: number;
3434
task: string;
35-
cancelToken?: Token;
35+
cancelToken?: Token | undefined;
3636
payload?: any;
3737
};
3838

tests/typetests/index.test.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,37 @@ describe("Type Tests", () => {
4545

4646
type TaskTypes = WorkerTasks<TestTasks>;
4747

48-
expect<TaskTypes['add']['payload']>().type.toBe<{ a: number; b: number }>();
49-
expect<TaskTypes['add']['return']>().type.toBe<number>();
50-
expect<TaskTypes['greet']['payload']>().type.toBe<{ name: string }>();
51-
expect<TaskTypes['greet']['return']>().type.toBe<string>();
52-
expect<TaskTypes['noArgs']['payload']>().type.toBe<undefined>();
53-
expect<TaskTypes['noArgs']['return']>().type.toBe<void>();
48+
expect<Pick<TaskTypes, "add">>().type.toBe<{
49+
add: { payload: { a: number; b: number }; return: number }
50+
}>();
51+
expect<Pick<TaskTypes, "greet">>().type.toBe<{
52+
greet: { payload: { name: string }; return: string }
53+
}>();
54+
expect<Pick<TaskTypes, "noArgs">>().type.toBe<{
55+
noArgs: { payload: undefined; return: void }
56+
}>();
5457
});
5558

5659
test("AsyncMessage should have correct structure", () => {
5760
type TestTasks = {
5861
compute: (args: { value: number }) => string;
5962
};
6063

61-
type Message = AsyncMessage<TestTasks>;
62-
63-
expect<Message['task']>().type.toBe<keyof TestTasks>();
64-
expect<Message['workItemId']>().type.toBe<number>();
65-
expect<Message['cancelToken']>().type.toBe<Token | undefined>();
66-
expect<Message['payload']>().type.toBe<{ value: number } | undefined>();
64+
expect<AsyncMessage<TestTasks>>().type.toBe<{
65+
task: "compute";
66+
workItemId: number;
67+
cancelToken?: Token | undefined;
68+
payload?: { value: number } | undefined;
69+
}>();
6770
});
6871

6972
test("CancellationSource static methods should have correct signatures", () => {
7073
const token: Token = new Int32Array(1);
7174

72-
expect(CancellationSource.isSignaled).type.toBeCallableWith(token);
7375
expect(CancellationSource.isSignaled(token)).type.toBe<boolean>();
7476

75-
expect(CancellationSource.throwIfSignaled).type.toBeCallableWith(token);
76-
expect(CancellationSource.throwIfSignaled).type.toBeCallableWith(undefined);
7777
expect(CancellationSource.throwIfSignaled(token)).type.toBe<void>();
78+
expect(CancellationSource.throwIfSignaled(undefined)).type.toBe<void>();
7879
});
7980

8081
test("Queue should not accept wrong types", () => {
@@ -84,4 +85,4 @@ describe("Type Tests", () => {
8485
expect(stringQueue.enqueue).type.not.toBeCallableWith(true);
8586
expect(stringQueue.enqueue).type.not.toBeCallableWith({});
8687
});
87-
});
88+
});

tests/typetests/tsconfig.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
2626

2727
/* Modules */
28-
"module": "ESNext", /* Specify what module code is generated. */
28+
"module": "NodeNext", /* Specify what module code is generated. */
2929
// "rootDir": "./", /* Specify the root folder within your source files. */
30-
"moduleResolution": "bundler", /* Specify how TypeScript looks up a file from a given module specifier. */
30+
"moduleResolution": "nodenext", /* Specify how TypeScript looks up a file from a given module specifier. */
3131
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
3232
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
3333
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
@@ -57,7 +57,7 @@
5757
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
5858
// "outDir": "./dist", /* Specify an output folder for all emitted files. */
5959
// "removeComments": true, /* Disable emitting comments. */
60-
// "noEmit": true, /* Disable emitting files from a compilation. */
60+
"noEmit": true, /* Disable emitting files from a compilation. */
6161
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
6262
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
6363
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
@@ -67,7 +67,7 @@
6767
// "newLine": "crlf", /* Set the newline character for emitting files. */
6868
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
6969
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
70-
"noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
70+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
7171
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
7272
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
7373

@@ -92,10 +92,10 @@
9292
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
9393
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
9494
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
95-
"exactOptionalPropertyTypes": false, /* Interpret optional property types as written, rather than adding 'undefined'. */
95+
"exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
9696
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
9797
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
98-
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
98+
"noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
9999
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
100100
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
101101
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */

0 commit comments

Comments
 (0)