-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Suggestion: compiler flag to prevent functions returning non-void
being assigned to functions returning void
#53421
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
Comments
Is there a reason you donβt think this agrees with the design goals? |
I wasn't sure, but after thinking about it some more, I've now checked that box. |
There's no way this would be workable in practice. The defining feature of void (relative to The example itself doesn't really manifest any type violations, so trying to fix it with a type system change seems off. You could be writing, for example, this: const log =
(msg: string): Lazy<undefined> => // <- more-precise return type
() =>
void console.log(msg);
// I did mean to do this; no error has occurred
const fn1 = match(log("foo"), () => log("This will not be logged."));
const a = fn1();
if (a) {
a();
} A possible flag would be to treat unreturning function bodies as returning |
One big inconvenience with |
In TypeScript 5.1 not anymore, see #36288. It already works in nightly: Playground link |
Imagine we were annotating const log =
(msg: string): Lazy<void> =>
() =>
void console.log(msg);
const fn1: Lazy<void> = match(log("foo"), () =>
log("This will not be logged.")
); If we replace const log =
(msg: string): Lazy<undefined> =>
() =>
void console.log(msg);
// Type 'Lazy<undefined>' is not assignable to type 'undefined'.
const fn1: Lazy<undefined> = match(log("foo"), () =>
log("This will not be logged.")
); So, I think the flag you're describing would be useful (in the case where |
Suggestion
π Search Terms
void
match
IO
typeLazy
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
A compiler flag to prevent functions returning non-
void
being assigned to functions returningvoid
.π Motivating Example
Playground
π» Use Cases
See the above motivating example. This is a reduced version of bug we had on Unsplash (using the
IO
type in fp-ts instead ofLazy
).It's possible to catch these bugs using
no-discarded-pure-expression
, however this lint rule is extremely slow.The text was updated successfully, but these errors were encountered: