Giter VIP home page Giter VIP logo

eslint-plugin-strict-null-checks's People

Contributors

dliblik avatar gp4ck avatar jaroslawpokropinski avatar qnku avatar thebox193 avatar wrager avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

eslint-plugin-strict-null-checks's Issues

"Passing nullable argument to function that expects non-nullable" for an any typed function argument

Hi!

I came across an interesting scenario and was curious whether it was the plugin or typescript.

For the following function, arg1 shows an eslint error.

image

Looking at the message it says "Passing nullable argument to function that expects non nullable":

image

The type definition of Array locally shows that the argument is typed as any isArray(arg: any): arg is any[]; but it's still flagged as non-nullable.

Have you come across this before or do you have any hints where I can start looking? Thank you!!

Plugin does not work with functions

Hej,

In this case, I assumed plugin should highlight that fn is potentially undefined but it does not.

type Fn = (() => void) | undefined;

const boo = (fn?: Fn) => {
	fn();
};

boo();

Plugin does not work with react props

this code should show an error in TestComponent where it tries to pass the nullable someProp on to the child component, where it is not nullable:

const ChildComponent = ({ someProp }: { someProp: SomeType }) => {
  return <>{someProp.name}</>;
};

const TestComponent = ({ someProp }: { someProp: SomeType | null }) => {
  return <ChildComponent someProp={someProp} />;
};

It should be noted that using a strictNullChecks in the global tsconfig does catch this issue, but since we're attempting to migrate incrementally, that won't be a good solution for us.

Getting "[object Object]" warnings

In multiple occasions the warning eslint (with rule strict-null-checks/all) gives me returns "[object Object]" as the message, which is a little confusing.

Example:

const temp: () => Promise<string> = async () => {
  const test: {
    name: string | null;
  } = {
    name: null,
  };

  return test.name;
};

export default temp;

Running eslint over this file gives me:

temp.ts
  1:7  warning  [object Object]  strict-null-checks/all

✖ 1 problem (0 errors, 1 warning)

Multiple projects

I have multiple projects in my repository with the following structure. I was trying to get this plugin working in both root level (for all of individual projects) or even for single project in src folder. In both cases, without success. I'm receiving no warning messages even though I should.

my_project/
├── libs/
└── src/
    ├── app/
    ├── .eslintrc.json
    ├── tsconfig.json
    ├── tsconfig.app.json
    └── tsconfig.strictNullChecks.json
.eslintrc.json
tsconfig.json

current configuration:

// my_project/src/.eslintrc.json

{
  "extends": ["../.eslintrc.json"],
  "overrides": [
    {
      "files": [ "*.ts" ],
      "parserOptions": {
        "project": [
          "src/tsconfig.strictNullChecks.json"
        ]
      },
      "plugins": [ "strict-null-checks" ],
      "rules": {
        "strict-null-checks/all": "warn"
      }
    }
  ]
}
// my_project/src/tsconfig.strictNullChecks.json

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}
// my_project/src/tsconfig.json

{
  "extends": "../tsconfig.json",
  "include": [],
  "files": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
  ]
}
// my_project/src/tsconfig.app.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "strictNullChecks": false,
  },
  "files": [
    "main.ts",
    "polyfills.ts"
  ],
}

Any ideas how to get this plugin working? I can provide some minimalistic reproduction if needed. Thank you in advance!

Error when passing null as generic nullable argument

const fn1: (arg1: string | null) => void = (arg1: string | null): void => {};
fn1(null); // OK

type Action1<T> = (arg1: T) => void;
const fn2: Action1<string | null> = fn1;
fn2(null); // Error: Passing nullable argument to function that expects non nullable

E.g., it makes some troubles with using the React.useState hook:

const [x, setX] = useState<string>();
setX(undefined); // Error: Passing nullable argument to function that expects non nullable
// The actual type of setX is:
// (value: React.SetStateAction<string | undefined>) => void
// that expands to:
// (value: string | undefined | ((prevState: string | undefined) => string | undefined)) => void

"Assigning nullable value to non-nullable variable" when declaring a variable that will be given a value later

Not sure if it's an issue with this repo or with TS but here we go:

In my tests, I often have to declare a variable at the top, which I will use in the beforeAll and the afterAll.
Example:

describe('My test suite', () => {
  let app: Server;
  beforeAll(async () => {
    app = await Server.init();
  });
  afterAll(async () => {
    await app.close();
  });
});

The problem is that I get a warning on the line let app: Server; "Assigning nullable value to non-nullable variable".
Do you have a way around it?
I've also asked a question on SO: https://stackoverflow.com/questions/71490614/how-to-late-init-non-nullable-variables

[PERF] It's quite slow to run

This rule ended up taking up 75% of my linting time, out of quite a few rules.
Since I run the linter every time I save, I unfortunately did have to disable this one.
Is this normal?

Maximum call stack exceeded error crashing CI pipelines

Scenario

Sometimes TS just can't figure stuff out and this type of statement is the only way 'round:

// Force the conversion
const smReq = req as unknown as ISmRequest<TParams, TRequest>;

Issue

Unfortunately this causes an infinite recursion in compareTypeObjects here:

https://github.com/JaroslawPokropinski/eslint-plugin-strict-null-checks/blob/main/src/utils/compare.ts#L51

Workaround

The workaround (for others encountering this) is to alter the function declaration in the above file to add an extra level parameter...

function compareTypeObjects(left, right, checker, level) {

... and then increment it on each recursion and bail if the level hits a large number (I use 50) - notice that the call to compareTypeObjects must be augmented with an increment to level:

//...
        if (level > 50) {
          return true;
        }
        if (!compareTypeObjects(leftPropertyType, rightPropertyType, checker, (level||0)+1))
            return false;
//...

Assigning nullable value to non nullable variable - `For key in object`

It would appear that this particular message generates erroneously in the case of a for key in object pattern. For example:

const t = {
   a: 'a'
 };
for (const key in t) {
}

image

I do not believe in such cases that a warning should be displayed. This does not generate a TypeScript compiler warning.

Setting undefined on non-nullable property gives no hint

type Foo = {
  NullableString?: string
  NonNullableString: string
  NonNullableStringTwo: string
}

const bar = {
  NullableProperty: undefined,
}

const data: Foo = {
  NullableString: '',
  NonNullableString: bar.NullableProperty?.bla, // even though we have optional chaining, undefined will still be set, the compiler/linter should know this
  NonNullableStringTwo: bar.NullableProperty,
}

console.log(data)

In this example, tsc gives an error (as it should), but the eslint rule does not (since optional chaining would in theory give undefined, and set undefined on a non-nullable property)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.