Walkthrough
A tour of the features that set TSTyche apart.
Test Against Specific TypeScript Versions
TSTyche has everything needed to test against specific TypeScript versions:
tstyche --target 5.4The above command runs tests using TypeScript 5.4.5. You can specify a range of versions as well:
tstyche --target '>=5.6'To learn more, see the TypeScript Versions page.
Expect Errors
Testing invalid code with @ts-expect-error is a popular approach, but it casts too wide a net. Any error on the following line will make the test pass, even the wrong one. TSTyche ships with ability matchers like .not.toBeCallableWith() that communicate the intent of a test. For cases where the error message itself matters, TSTyche also reads the message after @ts-expect-error and verifies it matches the actual error.
To learn more, see the Expect Errors page.
Reject Unexpected any and never
When a type unexpectedly resolves to any or never, tests may still pass, silently masking the issue. TSTyche rejects these types automatically, without repetitive .not.toBe<any>() or .not.toBe<never>() checks.
To learn more, see the rejectAnyType and rejectNeverType options.
Generate Tests
When testing a type against many inputs, writing out each test by hand is repetitive. TSTyche can generate tests from a template file. Place the // @tstyche template directive at the top and export the test text as default:
// @tstyche template
let = `import { expect, test } from "tstyche";
`;
for (const of ["string", "number"]) {
+= `test("is ${} a string?", () => {
expect<${}>().type.toBe<string>();
});
`;
}
export default ;To learn more, see the Template Test Files page.
Run Programmatically
To run TSTyche from within a script or a runtime test, the tstyche/tag entry point provides a tagged template function with a zx inspired API.
import from "node:test";
import from "tstyche/tag";
("generate types", async () => {
const = (import.meta);
// ...
await `--quiet --root ${}`;
});To learn more, see the Programmatic Usage page.
Integrate into Unit Tests
When a test requires complex setup, duplicating it in a separate type test file adds unnecessary overhead. TSTyche assertions can be mixed directly into existing unit tests:
export function (: number) {
if (typeof === "number" && !.()) {
return * 1000;
}
throw new ("Not a number");
}import from "node:assert";
import from "node:test";
import * as from "tstyche";
import { } from "../toMilliseconds.js";
("toMilliseconds", () => {
const = (10);
.(, 10_000);
.()..<number>();
// Will pass as a type test and not throw at runtime
.()...("20");
});Use the testFileMatch configuration option to include files in the test run:
{
"testFileMatch": ["**/*.test.ts", "**/*.tst.ts"]
}TSTyche assertions and testing helpers are empty functions returning undefined. They have no effect when invoked by a JavaScript test runner.