Skip to content

globalAssignments

Reports attempting to assign to read-only global variables such as undefined, NaN, Infinity, Object, etc.

✅ This rule is included in the ts untyped presets.

Global variables like undefined, NaN, Infinity, and built-in objects like Object and Array are read-only and should not be modified. Attempting to reassign these globals can lead to confusing behavior and potential bugs in your code. In strict mode, reassigning these globals will throw a TypeError at runtime.

undefined = 1;
Error ts(2539) ― Cannot assign to 'undefined' because it is not a variable.
var NaN: number
NaN
= 42;
var Infinity: number
Infinity
= 100;
Object = null;
Error ts(2322) ― Type 'null' is not assignable to type 'ObjectConstructor'.
Array = function () {};
Error ts(2739) ― Type '() => void' is missing the following properties from type 'ArrayConstructor': isArray, from, of, [Symbol.species]
let
let count: number
count
= 5;
undefined +=
let count: number
count
;
Error ts(2539) ― Cannot assign to 'undefined' because it is not a variable.
var NaN: number
NaN
++;
--
var Infinity: number
Infinity
;

This rule is not configurable.

If your codebase intentionally assigns to global objects and is guaranteed to run in an environment that allows that, you might not want to enable this rule. For example, if you target a legacy runtime with legacy non-standard JavaScript semantics, modern standard practices may not apply to you.

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