Skip to content

Commit

Permalink
feat: add a readonly properties handling eslint rule
Browse files Browse the repository at this point in the history
  • Loading branch information
galargh committed Nov 22, 2024
1 parent 05ae2b3 commit 4550dda
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config-v-next/eslint.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ function createConfig(
message:
"Use the conditionalUnionType or unionType helpers from the zod utils package instead, as it provides better error messages.",
},
{
// This rule ensures that non-primitive readonly properties are typed
// as Readonly<T>. This prevents their properties from being modified.
selector:
// Target properties on both classes/objects and interfaces
":matches(PropertyDefinition, TSPropertySignature)"
// Include only properties tagged as readonly
+ "[readonly=true]"
// Exclude properties already using the Readonly utility type
+ ":not(:has(TSTypeReference[typeName.name='Readonly']))"
// Exclude properties with primitive types
+ ":not(:has(TSTypeAnnotation > TSKeywordType))",
message: "Use Readonly<T> instead of T for non-primitive readonly properties",
},
],
"@typescript-eslint/restrict-plus-operands": "error",
"@typescript-eslint/restrict-template-expressions": [
Expand Down

0 comments on commit 4550dda

Please sign in to comment.