Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Description
These rules are all implemented - one final case remains to be implemented:no-unsafe-argument
to catch when you're passingany
into an argument. (See#791 (comment))
Create a rule which uses type information to determine when you're inadvertently breaking type safety by using an any, potentially without knowing.
Often libraries (or even the typescript defs themselves) can have weak(/lazy) types which returnany
. If you're not careful, you can inadvertently introduceany
s within your codebase, leading to bugs that aren't caught by the compiler.
Examples:
constfoo=Object.create(null);// ^^^ error: variable declaration implicitly typed as anyconstbar=x.prop;// ^^^ error: variable declaration implicitly typed as anyconstbaz=(1asany);// ^^^ error: variable declaration implicitly typed as anyconstbuzz=[];// ^^^^ error: variable declaration implicitly typed as any[]letbam:any=1;// no error, as it's **explicitly** anyfunctionfoo(arg:string){// ^^^^^^^^^^^^^^^^^^^^^^^^^ error: function return type is implicitly anyif(someCondition){return'str';}returnbam;// ^^^^^^^^^^^ error: return statement implicitly returns any}foo(x.prop);// ^^^^^^ error: passed function argument implicitly typed as anyfor(constfizzofbar){}// ^^^^^^^^^^ error: variable declaration implicitly typed as anyfor(leti=foo;i<10;i+=1){}// ^ error: variable declaration implicitly typed as any// variable decls, returns, arguments, assignment expressions, ifs...
Should also check assignments:
bam=foo;// ^^^^^^^^^^ error: assignment expression implicitly uses as anyconstclazz=newClazz();clazz.foo=foo;// ^^^^^^^^^^^^^^^^ error: assignment expression implicitly uses as anyfoo+=foo;// ^^^^^^^^^^^ error: assignment expression implicitly uses anyfoo++;// ^^^^^^ error: update expression implicitly uses any
Maybe also check things boolean things like if/while/etc?
if(foo){}// ^^^ error: if condition implicitly typed as anywhile(foo){}// ^^^ error: while condition implicitly typed as anydo{}while(foo)// ^^^ error: do...while condition implicitly typed as anyswitch(foo){// ^^^ error: switch discriminate implicitly typed as anycasebar:// ^^^ error: case test implicitly typed as anybreak;}
Should also provide an option to go strict with property access to help with finding the source of an any via property access):
typeObj={foo:{[k:string]:any;bar:{baz:string;};};};declareconstx:Obj;consty=x.foo.bam.baz;// ^^^^^^^^^ error: property access implicitly returns any// ^ error: variable declaration implicitly typed as anyconstz:string=x.foo.bam.baz;// ^^^^^^^^^ error: property access implicitly returns anyconst{ baz}=x.foo.bam;// ^^^^^^^^^ error: property access implicitly returns any// ^^^ error: destructured variable implicitly typed as any