Installations
npm install ts-expect
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
15.8.0
NPM Version
7.5.3
Score
99.6
Supply Chain
100
Quality
75.5
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
TypeStrong
Download Statistics
Total Downloads
18,111,540
Last Day
9,523
Last Week
49,223
Last Month
467,606
Last Year
6,794,764
GitHub Statistics
216 Stars
36 Commits
10 Forks
15 Watching
2 Branches
13 Contributors
Bundle Size
321.00 B
Minified
235.00 B
Minified + Gzipped
Package Meta Information
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
Publised On
13 Feb 2021
Total Downloads
Cumulative downloads
Total Downloads
18,111,540
Last day
-12.1%
9,523
Compared to previous day
Last week
-52.6%
49,223
Compared to previous week
Last month
-10.3%
467,606
Compared to previous month
Last year
38.3%
6,794,764
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
TS Expect
Checks values in TypeScript match expectations.
Installation
1npm install ts-expect --save
Usage
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!
How does this work?
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.
Testing definitions
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);
Exhaustive checks
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).
Exported Types
TS Expect comes with some utility types built-in to make testing easier. File an issue if you think something is missing!
TypeEqual<Target, Value>
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.
TypeOf<Target, Value>
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
.
Prior Works
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 functions
License
MIT
No vulnerabilities found.
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
8 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-m95q-7qp3-xv42
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
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 11 are checked with a SAST tool
Score
3
/10
Last Scanned on 2025-01-13
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 MoreOther packages similar to 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