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

Allow Boolean() to be used to perform a null check #29955

Merged
merged 3 commits into from Apr 30, 2019

Conversation

ForbesLindesay
Copy link
Contributor

@ForbesLindesay ForbesLindesay commented Feb 18, 2019

Fixes #16655

This allows code like:

const nullableValues = ['a', 'b', null];
const values: string[] = nullableValues.filter<string>(Boolean);

to be written and to typecheck. Unfortunately the generic type still has to be specified, otherwise TypeScript picks the other, less specific overload of .filter.

@msftclas
Copy link

msftclas commented Feb 18, 2019

CLA assistant check
All CLA requirements met.

@beshanoe
Copy link

beshanoe commented Mar 5, 2019

Hi, this is a really good initiative, do you think we can skip specifying generic after this
#30215 is merged?

@RyanCavanaugh
Copy link
Member

@typescript-bot test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 5, 2019

Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at 24e7e1c. You can monitor the build here. It should now contribute to this PR's status checks.

@ForbesLindesay
Copy link
Contributor Author

@beshanoe I think that may be sufficient to allow us to skip specifying generic parameters. I'm not sure though without testing it.

I found:

function B<T>(value: T): value is Exclude<T, false | null | undefined | '' | 0> {
  return Boolean(value);
}

was already sufficient, but adding prototypes/new functions to the interface forces you to be explicit.

@RyanCavanaugh
Copy link
Member

@ForbesLindesay can you merge this up with master? This will allow us to re-run the RWC suite to evaluate the effects of this on our real-world code database

@ForbesLindesay
Copy link
Contributor Author

@RyanCavanaugh sorry I didn't see your message. I've merged it now.

@ForbesLindesay
Copy link
Contributor Author

@RyanCavanaugh any update on this?

@RyanCavanaugh
Copy link
Member

@typescript-bot test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 28, 2019

Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at 37bb42c. You can monitor the build here. It should now contribute to this PR's status checks.

@RyanCavanaugh
Copy link
Member

RWC is actually clean, ignore ❌

@ForbesLindesay
Copy link
Contributor Author

@RyanCavanaugh any chance we can merge this? It's been a while and it's a very simple fix.

@victorouse
Copy link

victorouse commented Apr 13, 2019

@RyanCavanaugh any chance we can merge this? It's been a while and it's a very simple fix.

☝️ 🙏

@jack-williams jack-williams mentioned this pull request Apr 29, 2019
5 tasks
@pelotom
Copy link

pelotom commented Apr 29, 2019

Resolves #31164.

@silentroach
Copy link

broken by #31515

@city41
Copy link

city41 commented Nov 6, 2019

This would be a great addition to the language. Have there been any plans on when it might land? 3.8 maybe?

@pret-a-porter
Copy link

@city41 Unfortunately it is still open #16655 Really strange thing, that behaviour of !!someValue differs from Boolean(someValue)

@ShanonJackson
Copy link

@RyanCavanaugh

Can we revive this. Still continues to prevent usage of the .filter(Boolean) syntax sugar today.

@pelotom
Copy link

pelotom commented Feb 22, 2023

@RyanCavanaugh

Can we revive this. Still continues to prevent usage of the .filter(Boolean) syntax sugar today.

Check out ts-reset.

@stevage
Copy link

stevage commented Mar 10, 2023

Is there a good workaround in the meantime? I'm not sure how else to express .filter(Boolean) in a way Typescript accepts.

@jakebailey
Copy link
Member

Is there a good workaround in the meantime? I'm not sure how else to express .filter(Boolean) in a way Typescript accepts.

You can use type guards:

["", undefined, "test"].filter((x): x is string => typeof x === "string")

Playground Link

@ShanonJackson
Copy link

.filter((x): x is NonNullable<typeof x> => Boolean(x))

Works universally for any Array<T>

@jonny133
Copy link

jonny133 commented Nov 6, 2023

.filter((x): x is NonNullable<typeof x> => Boolean(x))

Works universally for any Array<T>

The code in #29955 (comment) more completely shows the result of Boolean excludes other falsey values

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 this pull request may close these issues.

Boolean() cannot be used to perform a null check