Gathering detailed insights and metrics for ts-expect
Gathering detailed insights and metrics for ts-expect
Gathering detailed insights and metrics for ts-expect
Gathering detailed insights and metrics for ts-expect
eslint-plugin-expect-type
ESLint plugin with ^? Twoslash, $ExpectError, and $ExpectType type assertions. 🧩
@pristine-ts/event
Compare to the networking module, the event module doesn't expect you to return a response, but simply to react to events.
ts-expect-error-validator
Since ts-expect-error does not have the ability to specify only the errors that we want to ignore, and instead suppresses all errors, it makes managing errors more challenging. This package provides a command-line tool to validate expected TypeScript erro
sedest
Jest matchers for fp-ts data types
npm install ts-expect
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
20,753,500
Last Day
31,595
Last Week
180,499
Last Month
748,486
Last Year
7,183,480
MIT License
248 Stars
36 Commits
10 Forks
13 Watchers
2 Branches
13 Contributors
Updated on Apr 06, 2025
Minified
Minified + Gzipped
Latest Version
1.3.0
Package Id
ts-expect@1.3.0
Size
8.50 kB
NPM Version
7.5.3
Node Version
15.8.0
Published on
Feb 13, 2021
Cumulative downloads
Total Downloads
Last Day
54.6%
31,595
Compared to previous day
Last Week
3.6%
180,499
Compared to previous week
Last Month
10.6%
748,486
Compared to previous month
Last Year
28.6%
7,183,480
Compared to previous year
Checks values in TypeScript match expectations.
1npm install ts-expect --save
TS Expect exports a function, named expectType
, that does nothing at all. Instead, it depends on the TypeScript compiler and a generic to test the type of a "value" passed to expectType
is assignable to its generic in the type system.
1import { expectType } from "ts-expect"; 2 3expectType<string>("test"); 4expectType<number>(123); 5expectType<number>("test"); // Compiler error!
TypeScript generics allow you to pass any value that implements the generic type. In this case, we're defining the generic explicitly as we pass the value so any value that isn't implementing our type is rejected by the TypeScript compiler. It's really that simple! The technical implementation is just <T>(value: T) => void
.
TypeScript has a "top type" named unknown
and a "bottom type" named never
. Using the top type to check assignability would mean every value is accepted, and the bottom type would mean nothing is accepted (except never
itself). As a result, you probably wouldn't want to use unknown
because everything would pass that check.
A quick note on any
: it's an "off switch" for TypeScript. It acts as a magical every type, both a top and a bottom type. This means it's assignable to everything and passing an any
value to expectType
will always pass the check.
Use with built-in or custom TypeScript utility types to implement a simple testing framework for your type definitions. If it compiles, it's valid!
1import { expectType, TypeEqual } from "ts-expect"; 2import { add } from "./adder"; 3 4expectType<number>(add(1, 2)); 5expectType<TypeEqual<boolean, ReturnType<typeof add>>>(true); 6expectType<TypeEqual<[number, number], Parameters<typeof add>>>(true);
Use with TypeScript's type narrowing to test that value
is what you expect. If you expand SupportedValue
with other values in the future, it'll fail an expectType<never>
or expectNever
check because you haven't used all the possible values.
1import { expectNever } from "ts-expect"; 2 3type SupportedValue = "a" | "b"; 4 5function doSomething(value: SupportedValue) { 6 switch (value) { 7 case "a": 8 return true; 9 case "b": 10 return true; 11 default: 12 return expectNever(value); 13 } 14}
Tip: Use expectNever(value)
when you need to return never
(i.e. throw an error if the code runs), use expectType<never>(value)
when you want to do tests in your code and expect the actual expression to be executed (i.e. do type checks but ignore the runtime).
TS Expect comes with some utility types built-in to make testing easier. File an issue if you think something is missing!
Checks that Value
is equal to the same type as Target
. This is a stricter check that avoids issues with testing sub-types. If you want to verify that an object is identical shape, not just "implements" Target
, this is the type you need.
Checks that Value
is assignable to Target
. This is effectively the same as expectType<Type>(value)
, except it's implemented in the type system directly so you can use it to test types instead of values by checking the result is true
or false
.
Some great prior works have been mentioned after publishing this package:
dtslint
does type checks via comment directives and inspired this approach of using the compilertsd-check
is a CLI that runs the TypeScript type checker over assertionstype-plus
comes with various type and runtime TypeScript assertionsstatic-type-assert
exposes a similar API surface with some type assertion functionsMIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
security policy file detected
Details
Reason
license file detected
Details
Reason
Found 4/24 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
10 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-05-05
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More