question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature]: typesafe assertions

See original GitHub issue

🚀 Feature Proposal

this will always fail, but since typescript infers the type as unknown, there’s no compile error:

expect('foo').toBe(1) // no error
expect<string>('foo').toBe(1) // no error

Motivation

it will allow you to identify errors in your tests much earlier, speeding up development 🚀🚀🚀

Example

// the new and improved expect 🚀:
declare const expect: <T>(actual: T) => { toBe: (expected: T) => void}

// usage:
expect('foo').toBe(1) // TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.

Pitch

the current expect types make using typescript with jest pretty much useless. i think if jest is going to support typescript, it should support basic functionality like this

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Sep 29, 2022

Would love toBe to work as type predicate 👍 Not sure how feasible it is, tho

0reactions
mrazauskascommented, Dec 17, 2022

Actually, no need to have any option or to wrestle with the Compiler API and no need to involve ts-jest too:

import {expect} from 'jest-strictly-typed-expect';

Could work like this too (I did not try):

import type {StrictlyTypedMatchers} from 'jest-strictly-typed-matchers';

declare module 'expect' {
    interface Matchers<R> extends StrictlyTypedMatchers<R> {}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing AssertiveTS: A type-safe, fluent assertion library
Take a dive into AssertiveTS, an assertion library with a nice fluent style that ensures type-safety across your tests. You can use it...
Read more >
const assertions are the killer new TypeScript feature
When I am finding out about new features, I want to know the exact ailment this feature is going to cure. For my...
Read more >
Type Assertion in TypeScript - TutorialsTeacher
Type assertion allows you to set the type of a value and tell the compiler not to infer it. This is when you,...
Read more >
How to write type safe assertion functions in TypeScript?
TypeScript 3.7 has this new cool feature which enables us to write "assertion functions". For example, here is one of mine:
Read more >
typed-assert - NPM Package Overview - Socket.dev
typed-assert is both a compile-time and runtime assert library. It leverages the assertion function feature of TypeScript to help the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found