Closed earthlyreason closed 4 years ago
Thanks!
I looked into this with https://github.com/avajs/ava/issues/2330 but best I could tell it wouldn't actually work. I get the feeling they added this because it would work in certain scenarios, but it doesn't in many others.
If you were to get it working we'd definitely consider this (and AVA 3 assumes TypeScript 3.7 so that'd be fine), but for now I'm going to close this issue, for house keeping purposes.
Thanks for the reply, @novemberborn! (Which I am also :) )
I swear I searched issues first, but missed that one.
The complication with generics does appear to be a showstopper for is
-like assertions.
AFAICT, the simpler case of a truthiness test works as desired for the assert
function, but I understand that it's also desirable to keep the API consistent where possible.
Our team is just checking out AVA, and so far so good. Thanks for making it!
Background
TypeScript 3.7 introduces assertion functions. This is a control-flow-analysis (CFA) feature targeted at functions like several of those that AVA provides.
Example
Suppose that you have a
Result
union type that discriminates on whether itssuccess
property istrue
orfalse
.Currently, if you say
TS doesn't know that
result
can now be narrowed to half of the union, even though that's clearly the intent (and effect) of the assertion.Instead, you have to write
With assert signatures (which essentially implies the
if
block as written above), TS will know that after theassert
call,result
hasgood_stuff
and notbad_stuff
.Solution
For
assert
this can be accomplished through a simple change to theAssertAssertion
interface inindex.d.ts
:Stronger signatures are possible for the other functions. I first wanted to see whether this was a non-starter for some reason before digging into the others.
Note that you can't achieve the same thing with module augmentation. The following attempt
Results in error TS 2775
I believe this is because the type extension does not remove the existing overload, which returns
void
rather than an assertion. See https://github.com/microsoft/TypeScript/pull/33622Drawbacks
I always stay on the latest TypeScript version, and I haven't researched how this change would affect users who are pegged to earlier versions. This is the only downside I can think of.