Skip to content

assignmentOperatorShorthands

Prefer logical assignment operator shorthand expressions.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

ES2021 introduced logical assignment operators (||=, &&=, ??=) that combine logical operations with assignment. These shorthand operators make code more concise and express intent more clearly.

For example, a = a || b can be written as a ||= b. This pattern is common for providing default values or conditionally updating variables.

let
let value: number
value
= 0;
let value: number
value
=
let value: number
value
|| 1;
let
let config: any
config
: Config | undefined;
Error ts(2749) ― 'Config' refers to a value, but is being used as a type here. Did you mean 'typeof Config'?
let config: any
config
=
let config: any
config
??
const getDefaultConfig: any
getDefaultConfig
();
let
let enabled: boolean
enabled
= false;
let enabled: boolean
enabled
=
let enabled: false
enabled
&&
const isFeatureAvailable: any
isFeatureAvailable
();

This rule is not configurable.

If you need to support environments that do not have ES2021 support, you may need to avoid using logical assignment operators. You may also prefer the more explicit expanded form if you find it more readable.

Made with ❤️‍🔥 around the world by the Flint team and contributors.