Skip to content

New set of rules: no-unsafe-* #791

Closed
Closed
@bradzacher

Description

@bradzacher

These rules are all implemented - one final case remains to be implemented: no-unsafe-argument to catch when you're passing any 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 return any. If you're not careful, you can inadvertently introduce anys within your codebase, leading to bugs that aren't caught by the compiler.

Examples:

const foo = Object.create(null);
//    ^^^ error: variable declaration implicitly typed as any
const bar = x.prop;
//    ^^^ error: variable declaration implicitly typed as any
const baz = (1 as any);
//    ^^^ error: variable declaration implicitly typed as any
const buzz = [];
//    ^^^^ error: variable declaration implicitly typed as any[]

let bam: any = 1; // no error, as it's **explicitly** any

   function foo(arg: string) {
// ^^^^^^^^^^^^^^^^^^^^^^^^^ error: function return type is implicitly any
     if (someCondition) {
       return 'str';
     }

     return bam;
//   ^^^^^^^^^^^  error: return statement implicitly returns any
   }

foo(x.prop);
//  ^^^^^^ error: passed function argument implicitly typed as any

for (const fizz of bar) {}
//   ^^^^^^^^^^ error: variable declaration implicitly typed as any
for (let i = 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 any
const clazz = new Clazz();
      clazz.foo = foo;
//    ^^^^^^^^^^^^^^^^ error: assignment expression implicitly uses as any
    foo += foo;
//  ^^^^^^^^^^^ error: assignment expression implicitly uses any
    foo++;
//  ^^^^^^ error: update expression implicitly uses any

Maybe also check things boolean things like if/while/etc?

if (foo) {}
//  ^^^ error: if condition implicitly typed as any
while (foo) {}
//     ^^^ error: while condition implicitly typed as any
do { } while (foo)
//            ^^^ error: do...while condition implicitly typed as any
switch (foo) {
//      ^^^ error: switch discriminate implicitly typed as any
    case bar:
//       ^^^ error: case test implicitly typed as any
      break;
}

Should also provide an option to go strict with property access to help with finding the source of an any via property access):

type Obj = {
  foo: {
    [k: string]: any;
    bar: {
      baz: string;
    };
  };
};

declare const x: Obj;

const y = x.foo.bam.baz;
//        ^^^^^^^^^ error: property access implicitly returns any
//    ^ error: variable declaration implicitly typed as any

const z: string = x.foo.bam.baz;
//                ^^^^^^^^^ error: property access implicitly returns any

const { baz } = x.foo.bam;
//              ^^^^^^^^^ error: property access implicitly returns any
//      ^^^ error: destructured variable implicitly typed as any

Metadata

Metadata

Assignees

Labels

enhancement: new plugin ruleNew rule request for eslint-pluginhas prthere is a PR raised to close thispackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions