#How to block any?
45 messages · Page 1 of 1 (latest)
strict should already turn on noImplicitAny, so you don't need to specify it again.
TS does not ban usages of any, you should set up your ESLint to do it. So it seems like your ESLint isn't properly setup?
What does your .eslintrc look like, and how are you running ESLint?
@pale epoch module.exports = { env: { browser: false, es2022: true }, root: true, extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'], overrides: [], parser: '@typescript-eslint/parser', parserOptions: { ecmaVersion: 'latest', sourceType: 'module', project: ['./tsconfig.json'] }, plugins: ['@typescript-eslint'], rules: { '@typescript-eslint/no-unsafe-call': 'warn', '@typescript-eslint/no-explicit-any': 'error', indent: ['error', 'tab'], 'linebreak-style': ['error', 'unix'], quotes: ['error', 'single'], semi: ['error', 'never'], 'no-tabs': 0, 'object-curly-spacing': [2, 'never'], 'array-bracket-spacing': [2, 'never'], 'computed-property-spacing': [2, 'never'], 'brace-style': [2, '1tbs'], 'keyword-spacing': [2], 'eol-last': [2], 'no-trailing-spaces': [2], 'no-redeclare': 2, 'no-shadow': 2, properties: 0, camelcase: 0 } } I tested in the console with "npx eslint ./src/boot.js" and in neovim with a warning about "quotes": ['error', 'single']. Both are working as expected.
You need to set it up to be able to lint with type information, see: https://typescript-eslint.io/linting/typed-linting
looks to me like it's already set up for that
eslint config file was generated by eslint, i just added some rules i use.
Doesn't seem like it for me
Because it's using plugin:@typescript-eslint/recommended rather than plugin:@typescript-eslint/recommended-type-checked.
Without type information, ESLint can only operate on the AST level.
the parser and parserOptions.project fields are already set, right above rules
that's only for recommended rules, it doesn't affect rules you manually specify
Yes, and @typescript-eslint/no-explicit-any is not what they are looking for.
certain rules in ts-eslint require type info
recommended is the recommended rules sans the type aware rules
recommended-type-checked is the recommended rules including type aware rules
No explicit any only bans direct usages of any.
there's no "implicit any" rule because that's a tsconfig option
So i have to remove "recomended" and add "recomended-type-checked" ?
it's unrelated to the issue, but you should anyways.
Eg it bans you from writing code like:
const foo: any = 42
However it doesn't bans usages of any in general like:
const foo = JSON.parse("{}")
foo.use.non.existent.stuff.because.its.any()
try compiling the code and see if ts itself complains. this is a case ts handles, not ts-eslint.
if the cli tool reports the error, it may just be the ide lagging, in which case, restart the language server.
right, because that's what the tsconfig option does
ts-eslint simply does not have a specific rule for that
unrelatedly, the eslintrc is set up perfectly fine for ts-eslint's type-aware checking
Unless I'm misunderstanding, they are trying to prevent usages of any in general.
The noImplicityAny in tsconfig.json doesn't do that, and also serves a different purpose than ESLint's no-explicity-any.
TS noImplicitAny only bans:
function foo(x) {
x.is.now.any()
}
And ESLint no-explicit-any bans explicit usage of any like:
const foo: any = 42
uses of any are either implicit or explicit. enabling both options together is how you fully ban any.
Neither of which will protect you from JSON.parse() returning any, and that requires type information for ESLint to ban.
That's not enough.
you can't really ban any from code that isn't yours
JSON.parse is more of consuming any than using it
do you mean banning just using it?
i feel like we're not on the same page here, so just to clarify
Eh sure, whatever the wording, but the point is that's what they are trying to prevent, so TS noImplicityAny + ESLint no-explicit-any won't cut it, you have to turn on linting with type information with relevant rules.
there are no relevant rules outside those
and that eslintrc is already geared for type-aware rules
Rules like no-unsafe-call, no-unsafe-argument, etc all bans usages of any.
They are not included in plugin:@typescript-eslint/recommended but they are included in plugin:@typescript-eslint/recommended-type-checked.
Because those rules simply cannot function unless you have type information.
i must be misremembering the scope of noImplicitAny then, mb.
yeah, no-unsafe-* are the rules for that
(but again, they're consumption, separately from giving types, which is what noImplicitAny and no-explicit-any handle)
Yeah, basically all 3 will prevent any from leaking.