Skip to content
项目

关于

Everything You Need for Type Testing.

开发工具

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
+1
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

TSTyche

version license install-size coverage

Everything You Need for Type Testing.


TSTyche is a type testing tool for TypeScript. It ships with describe() and test() helpers, expect style assertions and a mighty test runner.

Helpers

If you are used to testing, a type test should look familiar:

import { expect, test } from "tstyche";

function isSameLength<T extends { length: number }>(a: T, b: T) {
  return a.length === b.length;
}

test("isSameLength", () => {
  expect(isSameLength([1, 2], [1, 2, 3])).type.toBe<boolean>();
  expect(isSameLength("one", "two")).type.toBe<boolean>();

  expect(isSameLength).type.not.toBeCallableWith(1, 2);
});

TSTyche lets you organize tests using:

  • helpers: test(), it() and describe()
  • run mode flags: .only, .skip and .todo

Assertions

The expect style assertions can check either the inferred type of an expression or a type directly:

import { type _, expect } from "tstyche";

type AsyncProps<T> = {
  [K in keyof T]+?: T[K] | Promise<T[K]>;
};

type WithLoading<T extends object> = T & { loading: boolean };

expect<WithLoading<AsyncProps<{ query: string }>>>().type.toBe<{
  query?: string | Promise<string>;
  loading: boolean;
}>();

expect<WithLoading<_>>().type.not.toBeInstantiableWith<[string]>();

Relation matchers:

  • .toBe() checks if a type is the same as the given type
  • .toBeAssignableFrom() checks if a type is assignable from the given type
  • .toBeAssignableTo() checks if a type is assignable to the given type

Ability matchers:

  • .toAcceptProps() checks if a JSX component accepts the given props
  • .toBeApplicable checks if a decorator is applicable to the given class or class member
  • .toBeCallableWith() checks if a function is callable with the given arguments
  • .toBeConstructableWith() checks if a class is constructable with the given arguments
  • .toBeInstantiableWith() checks if a generic is instantiable with the given type arguments
  • .toHaveProperty() checks if a type has the given property

Runner

The tstyche command is the heart of TSTyche. It lets you select test files by path, filter tests by name and run them against specific versions of TypeScript:

tstyche query-params --only multiple --target '>=5.6'

That is just the beginning. It even lets you:

  • check messages of errors suppressed by @ts-expect-error directives
  • generate type tests from a template file
  • run tests in watch mode

Try It Out

Try TSTyche online on StackBlitz:

Open in StackBlitz

Documentation

Visit tstyche.org to view the full documentation.

Agent Skills

Skills for AI coding agents are available in the tstyche/tstyche-skills repository.

Roadmap

See ROADMAP.md for planned features.

License

MIT © TSTyche

探索相关项目

加载中…