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

Use with nullish coalescing operator #13

Open
Eli-Black-Work opened this issue May 7, 2020 · 2 comments
Open

Use with nullish coalescing operator #13

Eli-Black-Work opened this issue May 7, 2020 · 2 comments

Comments

@Eli-Black-Work
Copy link

Eli-Black-Work commented May 7, 2020

I have an additional example of where I think this feature would be useful: In conjunction with the nullish coalescing operator.

For example:

const car = cars[carType] ?? throw "Car type mayn't be null";

Would this be worth adding to the list of examples? :)

@tintin10q
Copy link

This is the main reason I would like to see this added great that @Bosch-Eli-Black already suggested this.

I would like to add this example when checking function arguments, which is even shorter.

function log_animal(animal) {
   animal ??= throw "Animal can not be undefined or null";
   animal.name ??= throw "Animal name can not be undefined or null";
   console.log(animal); 
}

@rbuckton
Copy link
Collaborator

I would like to add this example when checking function arguments, which is even shorter.

function log_animal(animal) {
   animal ??= throw "Animal can not be undefined or null";
   animal.name ??= throw "Animal name can not be undefined or null";
   console.log(animal); 
}

There's no need to use compound assignment here, animal ?? throw ... would work just as well.

I can add these to the explainer once #17 has merged.

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

No branches or pull requests

3 participants