First Steps
Write, run and fix your first type test.
Prerequisites
The minimum supported Node.js version is 22.12. It is recommended to use the latest LTS release .
TSTyche supports and tests only against the Long Term Support (LTS) releases of Node.js. Other releases might work too, but keep in mind that only the LTS releases are considered ready for general use.
Install TSTyche
Use your favorite package manager to add tstyche to a project:
npm
npm add -D tstycheBy default, TSTyche loads the locally installed TypeScript. When typescript is not installed, the latest version is used.
Write Your First Type Test
Create a MethodLikeKeys.ts file with the following content:
type = (...: any) => any;
export type <> = keyof {
[ in keyof as [] extends ? : never]: [];
};The MethodLikeKeys utility should construct a union type by picking up names of method-like keys of the T type.
Now create a MethodLikeKeys.tst.ts file in the same directory:
import { } from "tstyche";
import type { } from "./MethodLikeKeys.js";
interface Sample {
: string;
: () => number;
?: () => number;
}
<<Sample>>()..<"getLength" | "getWidth">();Here the expect() function defines an assertion. There are two method-like keys in the Sample interface, hence the resulting type must be "getLength" | "getWidth".
By convention, unit tests are suffixed with .test.* and type tests with .tst.*. To learn more, see the File Structure page.
Run the Type Test
Run TSTyche from your terminal:
npm
npx tstycheYou should see a report similar to this:
uses TypeScript 5.8.3 with baseline TSConfig
fail ./MethodLikeKeys.tst.ts
Error: Type '"getLength"' is not the same as type '"getLength" | "getWidth"'.
8 | }
9 |
10 | expect<MethodLikeKeys<Sample>>().type.toBe<"getLength" | "getWidth">();
| ~~~~~~~~~~~~~~~~~~~~~~~~
11 |
at ./MethodLikeKeys.tst.ts:10:44TSTyche does not execute the code. Instead, it analyzes types programmatically and reports the test run results. To learn more, see the Introduction page.
Fix and Re-run
The getWidth key is marked as optional and was not picked up by the MethodLikeKeys type. Wrapping the lookup with Required<T> ensures optional keys are not skipped:
export type <> = keyof {
[ in keyof as <>[] extends ? : never]: [];
};Run the test again:
uses TypeScript 5.8.3 with baseline TSConfig
pass ./MethodLikeKeys.tst.ts