declare-it
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

prettier-format: "true"

the-type-is-right

Node.js CI Coverage Status npm

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,

  1. The assertion that failed

  2. The assertion that failed is 𝗔𝗥𝗘_𝗡𝗢𝗧_𝗘𝗤𝗨𝗔𝗟 between number and string. It failed because number is not assignable to string

  3. 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

Package Sidebar

Install

npm i declare-it

Weekly Downloads

10

Version

0.1.0

License

MIT

Unpacked Size

34.9 kB

Total Files

55

Last publish

Collaborators

  • gregros