Skip to content
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
10 changes: 5 additions & 5 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test": "npm run test:unit && npm run test:types",
"test:unit": "ts-mocha -n loader=ts-node/esm -p ./tsconfig.json --require ./tests/setup.ts ./tests/ut/**/*.test.ts",
"test:watch": "ts-mocha -n loader=ts-node/esm -p ./tsconfig.json --require ./tests/setup.ts --watch --parallel --slow 100 ./tests/ut/**/*.test.ts",
"test:types": "tstyche ./tests"
"test:types": "tstyche"
},
"files": [
"dist/**/*.js",
Expand Down Expand Up @@ -45,7 +45,7 @@
"sinon-chai": "^4.0.1",
"ts-mocha": "^11.1.0",
"ts-node": "^10.9.2",
"tstyche": "^4.3.0",
"tstyche": "^5.0.0-beta.2",
"typescript": "^5.5.4",
"web-worker": "^1.5.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/misc/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Queue<TItem> {
return this.#tail++;
}

dequeue() {
dequeue(): TItem {
if (this.isEmpty) {
throw new Error("Cannot dequeue from an empty queue.");
}
Expand All @@ -31,7 +31,7 @@ export class Queue<TItem> {
return item;
}

peek() {
peek(): TItem {
if (this.isEmpty) {
throw new Error("Cannot peek on an empty queue.");
}
Expand Down
6 changes: 3 additions & 3 deletions src/workers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export type WorkerTasks<T extends Record<string, (...args: any) => any>> = {
export type AsyncMessage<Tasks extends Record<string, (...args: any) => any>> = {
task: keyof Tasks;
workItemId: number;
cancelToken?: Token;
payload?: WorkerTasks<Tasks>[keyof Tasks]['payload'];
cancelToken?: Token | undefined;
payload?: WorkerTasks<Tasks>[keyof Tasks]['payload'] | undefined;
};

/**
Expand All @@ -32,7 +32,7 @@ export type AsyncMessage<Tasks extends Record<string, (...args: any) => any>> =
export type AsyncMessageUntyped = {
workItemId: number;
task: string;
cancelToken?: Token;
cancelToken?: Token | undefined;
payload?: any;
};

Expand Down
33 changes: 17 additions & 16 deletions tests/typetests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,37 @@ describe("Type Tests", () => {

type TaskTypes = WorkerTasks<TestTasks>;

expect<TaskTypes['add']['payload']>().type.toBe<{ a: number; b: number }>();
expect<TaskTypes['add']['return']>().type.toBe<number>();
expect<TaskTypes['greet']['payload']>().type.toBe<{ name: string }>();
expect<TaskTypes['greet']['return']>().type.toBe<string>();
expect<TaskTypes['noArgs']['payload']>().type.toBe<undefined>();
expect<TaskTypes['noArgs']['return']>().type.toBe<void>();
expect<Pick<TaskTypes, "add">>().type.toBe<{
add: { payload: { a: number; b: number }; return: number }
}>();
expect<Pick<TaskTypes, "greet">>().type.toBe<{
greet: { payload: { name: string }; return: string }
}>();
expect<Pick<TaskTypes, "noArgs">>().type.toBe<{
noArgs: { payload: undefined; return: void }
}>();
});

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

type Message = AsyncMessage<TestTasks>;

expect<Message['task']>().type.toBe<keyof TestTasks>();
expect<Message['workItemId']>().type.toBe<number>();
expect<Message['cancelToken']>().type.toBe<Token | undefined>();
expect<Message['payload']>().type.toBe<{ value: number } | undefined>();
expect<AsyncMessage<TestTasks>>().type.toBe<{
task: "compute";
workItemId: number;
cancelToken?: Token | undefined;
payload?: { value: number } | undefined;
}>();
});

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

expect(CancellationSource.isSignaled).type.toBeCallableWith(token);
expect(CancellationSource.isSignaled(token)).type.toBe<boolean>();

expect(CancellationSource.throwIfSignaled).type.toBeCallableWith(token);
expect(CancellationSource.throwIfSignaled).type.toBeCallableWith(undefined);
expect(CancellationSource.throwIfSignaled(token)).type.toBe<void>();
expect(CancellationSource.throwIfSignaled(undefined)).type.toBe<void>();
});

test("Queue should not accept wrong types", () => {
Expand All @@ -84,4 +85,4 @@ describe("Type Tests", () => {
expect(stringQueue.enqueue).type.not.toBeCallableWith(true);
expect(stringQueue.enqueue).type.not.toBeCallableWith({});
});
});
});
12 changes: 6 additions & 6 deletions tests/typetests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "ESNext", /* Specify what module code is generated. */
"module": "NodeNext", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "bundler", /* Specify how TypeScript looks up a file from a given module specifier. */
"moduleResolution": "nodenext", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
Expand Down Expand Up @@ -57,7 +57,7 @@
// "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. */
// "outDir": "./dist", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
"noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
Expand All @@ -67,7 +67,7 @@
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
"noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */

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