You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// presence of dates on resource has already been checked so this assertion is safe
is that 3 months from now a fresh intern comes and inserts new code between the check and the assertion (not knowing that this assertion exists - it's hard to find even when looking for it specifically) and then 💥. Though, to be fair, it did not happen with js previously (due to lack of interns?)
Again, a small wrapper would make it safe and clean:
exportfunctionat<T>(a: T[],index: number): T{}
If you can find a fallback value, then something like this might work:
flatData[0]?.date??newDate()
This cannot fail and requires no hacks.
Continuing with the wrapper (though not directly applicable here sadly):
Another option, although more involved, is to write a custom array type which always returns value or throws and never returns undefined. There might be libraries implementing non-empty arrays.
The text was updated successfully, but these errors were encountered:
Make wrappers for common operations to avoid scattering the checks everywhere (there might be libs for that)
(Sadly, compilers are not yet smart enough to take array length checks into account. The length is only known at compile time, so in some cases it is plain impossible to decide at build time).
1
from #874 (comment)
nextstrain.org/static-site/src/components/ListResources/Modal.tsx
Lines 134 to 140 in aae874c
@ivan-aksamentov's recommendation:
Compiler is not smart enough to figure this out from this kind of the conditional. Connecting
.length
to.at()
is tough for a type system.What it can easily figure out is null checks. So a cleaner way would be to check and throw after non-fallible array access:
I would go further. Finding first and last value is a common enough "algorithm" that I would introduce a utility function:
This would make the code very pretty and safe, with all dirty details hidden:
2
from #874 (comment)
nextstrain.org/static-site/src/components/ListResources/Modal.tsx
Lines 148 to 156 in aae874c
nextstrain.org/static-site/src/components/ListResources/Modal.tsx
Lines 180 to 182 in aae874c
@ivan-aksamentov's recommendation:
The problem with comments like this:
// presence of dates on resource has already been checked so this assertion is safe
is that 3 months from now a fresh intern comes and inserts new code between the check and the assertion (not knowing that this assertion exists - it's hard to find even when looking for it specifically) and then 💥. Though, to be fair, it did not happen with js previously (due to lack of interns?)
Again, a small wrapper would make it safe and clean:
If you can find a fallback value, then something like this might work:
This cannot fail and requires no hacks.
Continuing with the wrapper (though not directly applicable here sadly):
Another option, although more involved, is to write a custom array type which always returns value or throws and never returns undefined. There might be libraries implementing non-empty arrays.
The text was updated successfully, but these errors were encountered: