noShadow
Summary
Section titled “Summary”- Rule available since:
v2.0.0 - Diagnostic Category:
lint/nursery/noShadow - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
no-shadow - Same as
@typescript-eslint/no-shadow
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noShadow": "error" } } }}Description
Section titled “Description”Disallow variable declarations from shadowing variables declared in the outer scope.
Shadowing is the process by which a local variable shares the same name as a variable in its containing scope. This can cause confusion while reading the code and make it impossible to access the global variable.
See also: noShadowRestrictedNames
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const foo = "bar";if (true) { const foo = "baz";}code-block.js:3:10 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable shadows another variable with the same name in the outer scope.
1 │ const foo = “bar”;
2 │ if (true) {
> 3 │ const foo = “baz”;
│ ^^^
4 │ }
5 │
ℹ This is the shadowed variable, which is now inaccessible in the inner scope.
> 1 │ const foo = “bar”;
│ ^^^
2 │ if (true) {
3 │ const foo = “baz”;
ℹ Consider renaming this variable. It’s easy to confuse the origin of variables if they share the same name.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
Variable declarations in functions can shadow variables in the outer scope:
const foo = "bar";const bar = function () { const foo = 10;}code-block.js:3:11 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable shadows another variable with the same name in the outer scope.
1 │ const foo = “bar”;
2 │ const bar = function () {
> 3 │ const foo = 10;
│ ^^^
4 │ }
5 │
ℹ This is the shadowed variable, which is now inaccessible in the inner scope.
> 1 │ const foo = “bar”;
│ ^^^
2 │ const bar = function () {
3 │ const foo = 10;
ℹ Consider renaming this variable. It’s easy to confuse the origin of variables if they share the same name.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
Function argument names can shadow variables in the outer scope:
const foo = "bar";function bar(foo) { foo = 10;}code-block.js:2:14 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable shadows another variable with the same name in the outer scope.
1 │ const foo = “bar”;
> 2 │ function bar(foo) {
│ ^^^
3 │ foo = 10;
4 │ }
ℹ This is the shadowed variable, which is now inaccessible in the inner scope.
> 1 │ const foo = “bar”;
│ ^^^
2 │ function bar(foo) {
3 │ foo = 10;
ℹ Consider renaming this variable. It’s easy to confuse the origin of variables if they share the same name.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const foo = "bar";if (true) { const qux = "baz";}Options
Section titled “Options”ignoreTypeValueShadow
Section titled “ignoreTypeValueShadow”Default: true
When enabled, a value binding that shares its name with a type-only declaration (type alias or interface) is not flagged, since types and values occupy separate namespaces in TypeScript.
When set to false, those cases are flagged:
{ "linter": { "rules": { "nursery": { "noShadow": { "options": { "ignoreTypeValueShadow": false } } } } }}type Foo = number;function f(Foo: string) {}code-block.ts:2:12 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable shadows another variable with the same name in the outer scope.
1 │ type Foo = number;
> 2 │ function f(Foo: string) {}
│ ^^^
3 │
ℹ This is the shadowed variable, which is now inaccessible in the inner scope.
> 1 │ type Foo = number;
│ ^^^
2 │ function f(Foo: string) {}
3 │
ℹ Consider renaming this variable. It’s easy to confuse the origin of variables if they share the same name.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ignoreFunctionTypeParameterNameValueShadow
Section titled “ignoreFunctionTypeParameterNameValueShadow”Default: true
When enabled, parameter names in function type annotations
(e.g. (x: string) => void) can share names with outer variables
without being flagged.
When set to false, those cases are flagged:
{ "linter": { "rules": { "nursery": { "noShadow": { "options": { "ignoreFunctionTypeParameterNameValueShadow": false } } } } }}const test = 1;type Fn = (test: string) => typeof test;code-block.ts:2:12 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This variable shadows another variable with the same name in the outer scope.
1 │ const test = 1;
> 2 │ type Fn = (test: string) => typeof test;
│ ^^^^
3 │
ℹ This is the shadowed variable, which is now inaccessible in the inner scope.
> 1 │ const test = 1;
│ ^^^^
2 │ type Fn = (test: string) => typeof test;
3 │
ℹ Consider renaming this variable. It’s easy to confuse the origin of variables if they share the same name.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.