#How to block any?

45 messages · Page 1 of 1 (latest)

eternal lotus
#
"@typescript-eslint/no-explicit-any": "error"
strict: true,
noImplicitAny: true

And then in the editor, it's ok. And code compiles, like nothing happened? Like wtf, why is this default behavior? What's the point of types, if you can type like this?

pale epoch
#

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?

eternal lotus
# pale epoch `strict` should already turn on `noImplicitAny`, so you don't need to specify it...

@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.

pale epoch
robust chasm
eternal lotus
#

eslint config file was generated by eslint, i just added some rules i use.

pale epoch
#

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.

robust chasm
#

the parser and parserOptions.project fields are already set, right above rules

robust chasm
pale epoch
#

Yes, and @typescript-eslint/no-explicit-any is not what they are looking for.

robust chasm
#

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

pale epoch
#

No explicit any only bans direct usages of any.

robust chasm
#

there's no "implicit any" rule because that's a tsconfig option

eternal lotus
#

So i have to remove "recomended" and add "recomended-type-checked" ?

robust chasm
pale epoch
#

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()
robust chasm
robust chasm
#

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

pale epoch
#

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
robust chasm
pale epoch
#

Neither of which will protect you from JSON.parse() returning any, and that requires type information for ESLint to ban.

robust chasm
#

you can't really ban any from code that isn't yours

pale epoch
#

You can

#

ESLint with type information can lint that.

robust chasm
#

JSON.parse is more of consuming any than using it

robust chasm
#

i feel like we're not on the same page here, so just to clarify

pale epoch
#

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.

robust chasm
#

there are no relevant rules outside those

#

and that eslintrc is already geared for type-aware rules

pale epoch
#

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.

robust chasm
#

thonk 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)

pale epoch
#

Yeah, basically all 3 will prevent any from leaking.