Package detail

tsd-lite

mrazauskas66.8kMITdeprecated0.9.0

'tsd-lite' is deprecated. For details, see the deprecation notice: https://github.com/mrazauskas/tsd-lite/issues/364

Test your TypeScript types easily

typescript, tsd, easy, check

readme

[!CAUTION]

tsd-lite is deprecated. It is recommended to migrate to TSTyche.

For details, see the deprecation notice.


tsd-lite

Test your TypeScript types easily.

version license node-ci

This is a lighter version of tsd. Slightly reworked codebase allows tsd-lite to be a tool which simply tests your types.

Motivation

While tsd suites perfectly for JavaScript libraries which declare their types in .d.ts files, its usage with monorepos written in TypeScript may become cumbersome. tsd-lite is an attempt to address these and similar issues.

Differences from tsd

  • tsd-lite performs only type testing without any additional checks or rules.
  • Exposes only general type related assertions: expectAssignable, expectNotAssignable, expectError, expectType and expectNotType. All other APIs (like expectNever, expectDeprecated, expectDocCommentIncludes and printType) are not implement.
  • Comes with no default compiler options.
  • Reads TypeScript compiler options from the nearest tsconfig.json for each test file (does not read options from package.json).
  • tsd-lite is optionally strict. You should add "strict": true to the nearest tsconfig.json (it can be project or test specific) to use strict assertions.
  • @tsd/typescript package is moved to peer dependencies.
  • tsd-lite allows only programmatic usage. For an integration with Jest see jest-runner-tsd, if you prefer standalone CLI implementation check tsd-lite-cli.

Install

yarn add -D tsd-lite @tsd/typescript
# or
npm install -D tsd-lite @tsd/typescript

Remember to install @tsd/typescript. It is a required peer dependency.

Assertions

The library provides the following type testing assertions.

expectAssignable<T>(expression)

Asserts that the type of expression is assignable to type T.

expectNotAssignable<T>(expression)

Asserts that the type of expression is not assignable to type T.

// JsonObject.ts
type JsonValue = string | number | boolean | JsonObject | Array<JsonValue>;

export interface JsonObject {
  [key: string]: JsonValue;
}
// __typetests__/JsonObject.test.ts
import { expectAssignable, expectNotAssignable } from "tsd-lite";
import type { JsonObject } from "../JsonObject.js";

expectAssignable<JsonObject>({
  caption: "test",
  count: 100,
  isTest: true,
  location: { name: "test", start: [1, 2], valid: false, x: 10, y: 20 },
  values: [0, 10, 20, { x: 1, y: 2 }, true, "test", ["a", "b"]],
});

expectNotAssignable<JsonObject>({
  filter: () => {},
});

expectType<T>(expression)

Asserts that the type of expression is identical to type T.

expectNotType<T>(expression)

Asserts that the type of expression is not identical to type T.

// MethodLikeKeys.ts
type FunctionLike = (...args: any) => any;

export type MethodLikeKeys<T> = keyof {
  [K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K];
};
// __typetests__/MethodLikeKeys.test.ts
import { expectType, expectNotType } from "tsd-lite";
import type { MethodLikeKeys } from "../MethodLikeKeys.js";

interface FixtureInterface {
  methodA?: ((a: boolean) => void) | undefined;
  methodB: (b: string) => boolean;

  propertyA?: number | undefined;
  propertyB?: number;
  propertyC: number | undefined;
  propertyD: string;
}

declare const interfaceMethods: MethodLikeKeys<FixtureInterface>;

expectType<"methodA" | "methodB">(interfaceMethods);
expectNotType<"methodA" | "methodB" | "propertyA">(interfaceMethods);

expectError(expression)

Asserts the expression has a type error.

// __typetests__/require-resolve.test.ts
import { expectError, expectType } from "tsd-lite";

// Expected 1-2 arguments
expectError(require.resolve());

// Returns a value of type 'string'
expectType<string>(require.resolve("tsd-lite"));

API Reference

The default export of the library is a function which takes fully resolved path to a test file as an argument:

import tsdLite from "tsd-lite";

const { assertionsCount, tsdResults } = tsdLite(
  "/absolute/path/to/testFile.test.ts",
);

It returns an object with assertionsCount and tsdResults properties:

{
  assertionsCount: number;
  tsdResults: Array<{
    messageText: string | ts.DiagnosticMessageChain;
    file?: ts.SourceFile;
    start?: number;
  }>;
}

tsd-lite will throw if the TypeScript compiler encounters an error while parsing tsconfig.json or finds a syntax error in the code.

License

MIT

changelog

Changelog

0.9.0 - 2024-02-24

🛑 tsd-lite is deprecated. 🛑

It is recommended to migrate to TSTyche. For details, see the deprecation notice.

0.8.2 - 2023-12-26

⚠️ tsd-lite is being sunset. ⚠️

It is recommended to migrate to TSTyche. For details, see the deprecation notice.

Fixed

  • Add ts2353 to the list of expected errors (#365)

0.8.1 - 2023-11-15

⚠️ tsd-lite is being sunset. ⚠️

It is recommended to migrate to TSTyche. For details, see the deprecation notice.

0.8.0 - 2023-05-21

Removed

  • Breaking: Remove the confusing expectError() type argument (#322)
  • Breaking: Remove the expectDeprecated() assertion (#320)
  • Breaking: Drop support for Node.js v14 and v19 (#319)

Fixed

  • Error on imports from 'tsd' package (#327)

0.7.0 - 2023-03-08

Removed

  • Breaking: Drop support for @tsd/typescript v3.x and add support for v5.x (#252)
  • Breaking: Drop support for Node.js v12 (finally) (#250)

Fixed

  • Add ts2375, ts2379 and ts2412 to the list of expected errors (#249)

0.6.0 - 2022-08-26

Removed

  • Breaking: Drop support for Node.js v12 (#110)

0.5.6 - 2022-06-30

Maintenance release: update CHANGELOG.md, README.md and package.json.

0.5.5 - 2022-06-30

Deprecated: missing build files.

0.5.4 - 2022-05-16

Fixed

  • Expose TsdResult type (#47)

0.5.3 - 2022-02-12

Fixed

  • Add ts2366 to the list of expected errors (#24)

0.5.2 - 2022-02-09

Fixed

  • Add ts2542 to the list of expected errors (#23)

0.5.1 - 2022-02-05

Fixed

  • Add ts2707 to the list of expected errors (#20)

0.5.0 - 2022-02-04

Fixed

  • Breaking: Add assertion logic for expectAssignable and expectType assertions (#17)

0.4.1 - 2022-01-17

Fixed

  • Resolve compiler options correctly if tsconfig.json is not found (#16)

0.4.0 - 2022-01-17

Changed

  • Breaking: Do not add flattened message to tsdResults object (#14)
  • Breaking: Throw error (instead of returning tsdErrors) (#13)

Fixed

  • Skip declaration files while extracting assertions (#15)

0.3.0 - 2022-01-14

Changed

  • Breaking: Do not silence diagnostics without location
  • Breaking: Do not silence diagnostics for files from node_modules directories

0.2.0 - 2022-01-12

Changed

  • Breaking: Reshape members of the returned object (#11)
  • Breaking: Return configDiagnostics alongside with tsdResults (#7)
  • Breaking: Return tsdResult instead of diagnostics (#6, #5)

Fixed

  • Fail early if syntax errors are found (#10)

0.1.0 - 2022-01-04

First release.