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

docs(contributing): add a guide for checking of undefined or null #1093

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ function startFoo() {
}
```

### Checking for `undefined` or `null`

In node-wot, we enabled [strict boolean expressions](https://typescript-eslint.io/rules/strict-boolean-expressions/). In summary, this means that in the
the code base is not allowed to use non-boolean expressions where a boolean is expected (see the [examples](https://typescript-eslint.io/rules/strict-boolean-expressions/#examples)).
How then should the contributor deal with nullable variables? For example:

```ts
function(arg1: string | null | undefined) {
// ERROR: not allowed by strict-boolean-expressions
if(!arg) { throw new Error("arg should be defined!))}
relu91 marked this conversation as resolved.
Show resolved Hide resolved
}
```

Instead of checking for both null and `undefiend` values (`if(arg !== undefined && arg !== null)`) the preferred solution is to use `!=` or `==` operator. Interestingly in JavaScript
with ==, null and undefined are only equal to each other. Example:
relu91 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have issues to understand the part "are only equal to each other".
Don't you want so say the sentence without "only" ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we can remove the only.


```ts
function(arg1: string | null | undefined) {
// OK
if(arg == null) { throw new Error("arg should be defined!))}
relu91 marked this conversation as resolved.
Show resolved Hide resolved
}
```

Further reading on the motivations can be found [here](https://basarat.gitbook.io/typescript/recap/null-undefined#checking-for-either).

## Commits

Eclipse Thingweb uses Conventional Changelog, which structure Git commit messages in a way that allows automatic generation of changelogs.
Expand Down
Loading