@typescript-eslint/no-magic-numbers applies to types #1972
Replies: 4 comments
-
Thank you for bringing this up, @Guillaume-Docquier . I am hereby considering. Say, is there an alternative other than ignoring the rule for the line? |
Beta Was this translation helpful? Give feedback.
-
I could use an enum. enum Rating {
VERY_BAD = 1,
BAD = 2,
AVERAGE = 3,
GOOD = 4,
VERY_GOOD = 5,
} Or I could derive the type from a constant, but I think it looks silly and it produces another error. const AVERAGE = 3; // ESLint: 'AVERAGE' is assigned a value but only used as a type.(@typescript-eslint/no-unused-vars)
export type Rating = 1 | 2 | typeof AVERAGE | 4 | 5; |
Beta Was this translation helpful? Give feedback.
-
The enum labels the numbers, preventing them from being magic numbers. Seems like a straightforward choice to me, in lieu of edge cases where enums are undesirable. What do you think? |
Beta Was this translation helpful? Give feedback.
-
It works in some cases, but not all. Or anything that is quantity based won't play well with enums because you'll end up doing I ended up reconfiguring this rule because I use type litterals a lot to restrict number ranges when I can and enums are not always suitable. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello! I'm trying out
eslint-config-love
and I'm facing issues with@typescript-eslint/no-magic-numbers
Consider the following code:
@typescript-eslint/no-magic-numbers
complains...I like the spirit of the rule, but to me
export type Rating
would be the equivalent ofconst SOMETHING = 9000
in type land.Would you consider
ignoreNumericLiteralTypes
?Beta Was this translation helpful? Give feedback.
All reactions