0

I have a problem that I think can be boiled down to this code snippet

public a() {
    return {a: 0};
}

public b() {
    return this.a()[0];
}

A while back the a function returned an array, but got refactored into returning a single object. It would have been great if I got an error on the b function, but as it stands right now it compiles happily and results in errors at runtime.

So even though I understand why it is valid code (accessing the zero key on the a object), I would like to have eslint or tsc warn me. But I have a hard time finding any rules/config that matches that. So is it at all possible?

Can add that this is the compilerOptions of my tsconfig

  "compilerOptions": {
    "skipLibCheck": true,
    "strict": true,
    "strictNullChecks": false,
    "typeRoots": ["./node_modules/@types", "./Script/typings"],
    "experimentalDecorators": true,
    "useDefineForClassFields": false,
    "esModuleInterop": true,
    "noImplicitAny": false,
    "module": "ES2020",
    "target": "ES2020",
    "lib": [
      "ES2021",
      "ES6",
      "DOM",
      "DOM.Iterable"
    ],
    "allowJs": false,
    "sourceMap": true,
    "moduleResolution": "node",
    "paths": {
      "/": [
        "./"
      ],
      "/*": [
        "./*"
      ]
    },
    "outDir": "build"
  }
5
  • 1
    TypeScript does complain about this though: Playground Commented Jun 28, 2024 at 5:13
  • 1
    This question is similar to: ESLint not reporting TypeScript compiler type checking errors. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jun 28, 2024 at 5:14
  • @Unmitigated that is quite interresting as it does not in my project. Meaning that there is likely some error in my config that causes it to not complain. Commented Jun 28, 2024 at 5:21
  • @RickyMo I see the similarities, but if I understand that question correctly, OPs typescript does throw errors that eslint does not catch. My case is different as neither tsc nor eslint is throwing errors for me Commented Jun 28, 2024 at 5:23
  • 3
    The cause is noImplicitAny: false, change it to true and you will get an error. Commented Jun 28, 2024 at 5:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.