declare-it v2.1.0
prettier-format: "true"
the-type-is-right
The Type Is Right is a TypeScript library for testing type declarations!
π§ͺ Write tests and assertions on types! π¨ Detailed and conspicuous failure messages! ποΈ Integrates with whatever runtime framework youβre using! π§ Rigorously tested! π‘ A familiar yet visually distinct API!
Install it:
yarn add the-type-is-right
npm install --save-dev the-type-is-right
Now put this in a test file:
import { declare_it, expect_type } from "@lib"
// Note that we don't give it a closure, just a bunch of assertions.
declare_it(
"checks that number is equal to string?",
expect_type<number>().to_equal<string>(),
expect_type<string>().to_equal<string>()
)
Now run tsc
, or just wait until something automatically compiles it for you. You get a compilation error:
Argument of type 'π§π’_ππ€π¨ππ_ππ₯π₯π’π₯<number, "π‘π’π§ π π¦π¨ππ§π¬π£π π’π", string>' is not assignable to parameter of type '"β π§ππ¦π§γ checks that number is equal to string? γβ"'.
The error is a bit confusing. You just need to ignore the boilerplate β information about the failure is encoded into the types themselves. Letβs take it apart.
π§π’_ππ€π¨ππ_ππ₯π₯π’π₯<number, "π‘π’π§ π π¦π¨ππ§π¬π£π π’π", string>
This is saying that there was an error in the to_equal
assertion. The error is that number
is not a subtype of string
.
β π§ππ¦π§γ checks that number is equal to string? γβ
This gives us the name of the test in which the failure occurred.
How to use it
There arenβt that many
Here, itβs saying that there was an error in the to_equal
assertion. The error is that number
The error says which assertion failed and which test it was part of. It even says why the assertion failed β because number
is not a subtype of string
.
The second type
The first type says what happened: π§π’ππ€π¨ππππ₯π₯π’π₯, that is, the
to_equal
assertion errored or failed.- The second type
The message is kind of confusing! But itβs actually easy to decode when you get used to it.
You just need to look at the types themselves. The first type describes the assertion that failed, while the second type gives the name of the test that failed.
In this case,
The assertion that failed
The assertion that failed is ππ₯ππ‘π’π§ππ€π¨ππ between
number
andstring
. It failed becausenumber
is not assignable tostring
An equality assertion failed doesnβt say outright that a test failed, but it does give you the important information:
Usage
This library
import { declare_test, expect_type } from "@lib"
What is
the type is right is a small library for compile-time type assertions. These type assertions have been carefully engineered to perfection, giving you the ideal set of tools to make sure that
- It doesnβt perform any runtime assertions or validation