Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toBeBoolean behaves differently from toEqualTypeOf<boolean> #32

Closed
boris-petrov opened this issue May 29, 2023 · 3 comments · Fixed by #16
Closed

toBeBoolean behaves differently from toEqualTypeOf<boolean> #32

boris-petrov opened this issue May 29, 2023 · 3 comments · Fixed by #16

Comments

@boris-petrov
Copy link

boris-petrov commented May 29, 2023

Playground link.

import { expectTypeOf } from 'expect-type';

let x!: any;
expectTypeOf(x).toBeBoolean();
expectTypeOf(x).toEqualTypeOf<boolean>();

The first expectTypeOf passes (which it should not) but the second errs (correctly). They should both err.

P.S. The same is true for other such helpers (like toBeArray, etc).

@mmkal
Copy link
Owner

mmkal commented Jun 2, 2023

Yes, I agree, this should really be a toEqualTypeOf rather than a toMatchTypeOf. It'd be a "breaking" change, but would probably only break anys that have crept into people's code, so that's probably a good thing (plus this library is still technically v0).

mmkal added a commit that referenced this issue Oct 1, 2023
@mmkal
Copy link
Owner

mmkal commented Oct 1, 2023

Note: some nuance here. I think this applies to primitive, but I don't think it makes sense for toBeFunction, toBeArray and toBeObject to use strict equality for now. It might make sense if helpers like toBeAFunction, toBeAnArray and toBeAnObject were added - but I would rather optimise for what I suspect is the most common/intuitive usage. Anyone who wants something else can pretty easily use toEqualTypeOf or toMatchTypeOf if necessary.

The primitive case will be fixed in #16.

@mmkal
Copy link
Owner

mmkal commented Oct 2, 2023

Copy-pasted from docs added in #16, this will be the new behaviour:


.toBe... methods allow for types which extend the expected type:

expectTypeOf<number>().toBeNumber()
expectTypeOf<1>().toBeNumber()

expectTypeOf<any[]>().toBeArray()
expectTypeOf<number[]>().toBeArray()

expectTypeOf<string>().toBeString()
expectTypeOf<'foo'>().toBeString()

expectTypeOf<boolean>().toBeBoolean()
expectTypeOf<true>().toBeBoolean()

.toBe... methods protect against any:

const goodIntParser = (s: string) => Number.parseInt(s, 10)
const badIntParser = (s: string) => JSON.parse(s) // uh-oh - works at runtime if the input is a number, but return 'any'

expectTypeOf(goodIntParser).returns.toBeNumber()
// @ts-expect-error - if you write a test like this, `.toBeNumber()` will let you know your implementation returns `any`.
expectTypeOf(badIntParser).returns.toBeNumber()

Note that this is different from .toEqualTypeOf<boolean>() - because expectTypeOf<true>().toEqualTypeOf<boolean>() would fail whereas expectTypeOf<true>().toBeBoolean() passes - but that's correct behaviour IMO. Let me know if you disagree.

@mmkal mmkal closed this as completed in #16 Oct 3, 2023
@mmkal mmkal closed this as completed in fd718dd Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants