Git Product home page Git Product logo

expect-type-extensions's Introduction

Expect Type Extensions

This package provides additional type testing methods for the expect-type compile-time test suite for types.

Goal

Sometimes, we want to test whether two types can be used interchangeably, i.e., ensure that while they may not be equal, they can be assigned to each other.

For example:

type Foo = { a: string, b: number }
type Bar = { a: string } & { b: number }

In practice, we can assign anything of type Foo to anything of type Bar and vice versa. So, from a practical point of view, these types are compatible. We naturally want to test for this kind of relationship between types.

Challenge

While expect-type is a great utility, it is a bit suboptimal for this particular case. We cannot use a single toEqualTypeOf, as both of these would result in an error:

expectTypeOf<Foo>().toEqualTypeOf<Bar>() // error, but we want a pass
expectTypeOf<Bar>().toEqualTypeOf<Foo>() // error, but we want a pass

This means we would need to resort to two toMatchTypeOf tests:

expectTypeOf<Foo>().toMatchTypeOf<Bar>()
expectTypeOf<Bar>().toMatchTypeOf<Foo>()

We need both lines because in the following case, only one of them shows the issue:

type Baz = { a: string, b: number, c: boolean }

expectTypeOf<Baz>().toMatchTypeOf<Foo>() // seems okay
expectTypeOf<Foo>().toMatchTypeOf<Baz>() // but this line shows the issue with Baz being incompatible

However, it can be a bit hard to read and maintain: always writing double test lines for this kind of scenario.

Solution

This utility provides a single .toMatchEachOther<>() method which can be used to test for this kind of relationship between types.

With this in place, we can now write what took us 4 tests before in only 2 lines:

expectTypeOf<Foo>().toMatchEachOther<Bar>() // pass
expectTypeOf<Foo>().toMatchEachOther<Baz>() // error, Baz is not compatible

If the second one is the expected behaviour, we can use the .not.toMatchEachOther<>() method instead:

expectTypeOf<Foo>().not.toMatchEachOther<Baz>() // pass

Usage

Installation

Install using npm or your choice of package manager, e.g.:

npm install --save-dev @sbnc/expect-type-extensions

Importing

To use the new utilities, make sure to import this package along with expect-type, e.g.:

import { expectTypeOf } from 'expect-type'
import '@sbnc/expect-type-extensions'

Note: There is no need for a {...} from part in the import statement.


Bence Szalai - https://sbnc.eu/

expect-type-extensions's People

Contributors

benceszalai avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.