A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://typescript-eslint.io/rules/no-unsafe-member-access below:

no-unsafe-member-access | typescript-eslint

no-unsafe-member-access

Disallow member access on a value with type any.

💭

This rule requires type information to run, which comes with performance tradeoffs.

The any type in TypeScript is a dangerous "escape hatch" from the type system. Using any disables many type checking rules and is generally best used only as a last resort or when prototyping code.

Despite your best intentions, the any type can sometimes leak into your codebase. Accessing a member of an any-typed value creates a potential type safety hole and source of bugs in your codebase.

This rule disallows member access on any variable that is typed as any.

eslint.config.mjs

export default tseslint.config({
rules: {
"@typescript-eslint/no-unsafe-member-access": "error"
}
});

.eslintrc.cjs

module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-member-access": "error"
}
};

Try this rule in the playground ↗

Examples
declare const anyVar: any;
declare const nestedAny: { prop: any };

anyVar.a;
anyVar.a.b;
anyVar['a'];
anyVar['a']['b'];

nestedAny.prop.a;
nestedAny.prop['a'];

const key = 'a';
nestedAny.prop[key];


const arr = [1, 2, 3];
arr[anyVar];
nestedAny[anyVar];
Open in Playground
declare const properlyTyped: { prop: { a: string } };

properlyTyped.prop.a;
properlyTyped.prop['a'];

const key = 'a';
properlyTyped.prop[key];

const arr = [1, 2, 3];
arr[1];
let idx = 1;
arr[idx];
arr[idx++];
Open in Playground Options

This rule is not configurable.

When Not To Use It

If your codebase has many existing anys or areas of unsafe code, it may be difficult to enable this rule. It may be easier to skip the no-unsafe-* rules pending increasing type safety in unsafe areas of your project. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.

Resources

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4