dot-notation
Enforce dot notation whenever possible.
Extending"plugin:@typescript-eslint/stylistic-type-checked" in anESLint configuration enables this rule.
Some problems reported by this rule are automatically fixable by the--fix ESLint command line option.
This rule requirestype information to run, which comes with performance tradeoffs.
This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. SeeRules > Extension Rules.
This rule is currentlyfrozen and is not accepting feature requests.
This rule extends the basedot-notation rule from ESLint core. It adds:
- Support for optionally ignoring computed
privateand/orprotectedmember access. - Compatibility with TypeScript's
noPropertyAccessFromIndexSignatureoption.
How to Use
- Flat Config
- Legacy Config
exportdefault tseslint.config({
rules:{
// Note: you must disable the base rule as it can report incorrect errors
"dot-notation":"off",
"@typescript-eslint/dot-notation":"error"
}
});
module.exports={
"rules":{
// Note: you must disable the base rule as it can report incorrect errors
"dot-notation":"off",
"@typescript-eslint/dot-notation":"error"
}
};
Try this rule in the playground ↗
Options
Seeeslint/dot-notation's options.
This rule adds the following options:
interfaceOptionsextendsBaseDotNotationOptions{
allowPrivateClassPropertyAccess?:boolean;
allowProtectedClassPropertyAccess?:boolean;
allowIndexSignaturePropertyAccess?:boolean;
}
const defaultOptions: Options={
...baseDotNotationDefaultOptions,
allowPrivateClassPropertyAccess:false,
allowProtectedClassPropertyAccess:false,
allowIndexSignaturePropertyAccess:false,
};
If the TypeScript compiler optionnoPropertyAccessFromIndexSignature is set totrue, then this rule always allows the use of square bracket notation to access properties of types that have astring index signature, even ifallowIndexSignaturePropertyAccess isfalse.
allowPrivateClassPropertyAccess
Whether to allow accessing class members marked asprivate with array notation. Default:false.
This can be useful because TypeScript will report a type error on dot notation but not array notation.
Example of a correct code whenallowPrivateClassPropertyAccess is set totrue:
classX{
private priv_prop=123;
}
const x=newX();
x['priv_prop']=123;
Open in PlaygroundallowProtectedClassPropertyAccess
Whether to allow accessing class members marked asprotected with array notation. Default:false.
This can be useful because TypeScript will report a type error on dot notation but not array notation.
Example of a correct code whenallowProtectedClassPropertyAccess is set totrue:
classX{
protected protected_prop=123;
}
const x=newX();
x['protected_prop']=123;
Open in PlaygroundallowIndexSignaturePropertyAccess
Whether to allow accessing properties matching an index signature with array notation. Default:false.
Example of correct code whenallowIndexSignaturePropertyAccess is set totrue:
classX{
[key:string]:number;
}
const x=newX();
x['hello']=123;
Open in PlaygroundIf the TypeScript compiler optionnoPropertyAccessFromIndexSignature is set totrue, then the above code is always allowed, even ifallowIndexSignaturePropertyAccess isfalse.
When Not To Use It
If you specifically want to use both member access kinds for stylistic reasons, or don't wish to enforce one style over the other, you can avoid this rule.
However, keep in mind that inconsistent style can harm readability in a project.We recommend picking a single option for this rule that works best for your project.
Type checked lint rules are more powerful than traditional lint rules, but also require configuringtype checked linting.
SeeTroubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.