Skip to content
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

void should be falsy #172

Closed
none23 opened this issue Oct 13, 2023 · 2 comments
Closed

void should be falsy #172

none23 opened this issue Oct 13, 2023 · 2 comments

Comments

@none23
Copy link

none23 commented Oct 13, 2023

Inside TSReset.NonFalsy, void is not considered falsy. That leads to incorrect behavior of .filter(Boolean)

declare namespace TSReset {
  type NonFalsy<T> = T extends false | 0 | '' | null | undefined | 0n 
    ? never
    : T;
}

interface Array<T> {
  filter(predicate: BooleanConstructor, thisArg?: any): TSReset.NonFalsy<T>[];
}

const fns = [
  (x?: unknown) => 1,
  () => {},
];

const nonEmptyResults = fns.map(fn => fn()).filter(Boolean)
//               ^? const nonEmptyResults: (number | void)[]

TS Playground Example

@russelldavis
Copy link

It's a nice idea, but making void falsy is not safe. Functions with a void return type can still return a truthy value. See https://www.typescriptlang.org/docs/handbook/2/functions.html#return-type-void

@none23
Copy link
Author

none23 commented Feb 5, 2024

@russelldavis Yeah, I guess that makes sense, thanks

@none23 none23 closed this as completed Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants