[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:
- Created a year ago
- Reactions:5
- Comments:16 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Would love
toBe
to work as type predicate 👍 Not sure how feasible it is, thoActually, no need to have any option or to wrestle with the Compiler API and no need to involve
ts-jest
too:Could work like this too (I did not try):