matrix-js-sdk icon indicating copy to clipboard operation
matrix-js-sdk copied to clipboard

Maybe enable `prefer-nullish-coalescing` and `prefer-optional-chain`

Open ShadowJonathan opened this issue 4 years ago • 2 comments

https://typescript-eslint.io/rules/prefer-nullish-coalescing/

https://typescript-eslint.io/rules/prefer-optional-chain/

These discourage falsy/truthy checking, and actually check for undefined or null.

This'd help if a parameter is number | null/idx?: number, to not branch on 0.

ShadowJonathan avatar Jan 23 '22 15:01 ShadowJonathan

Why?

turt2live avatar Jan 24 '22 18:01 turt2live

As explained in https://github.com/matrix-org/matrix-js-sdk/issues/2124, truthy/falsy values can lead to subtle bugs, and while the other issue deals with enforcing the abstention of using it in expressions, this issue converts any such case where a && a.b && a.b.c && a.b.c.d happens, and instead suggests to coalece it to a?.b?.c?.d, while not checking for truthy values inbetween, but explicitly for nullability.

Enabling it would help developers to more ergonomically and correctly write code that works with nullable variables, properties, functions, etc.

ShadowJonathan avatar Jan 24 '22 21:01 ShadowJonathan