0

Consider following example:

function myFunc (b: number): string {
  let a: { c: any }

  if (b < 20) a = { c: 100 }

  console.log(a.c) // depending on 'b' this may throw error
  return 'ok'
}

I want my linter to tell me about this particular problem - that variable may be undefined. What rules should I use? I tried: no-undef, no-undef-init, no-undefined and no-use-before-define

Tobias S.
  • 21,159
  • 4
  • 27
  • 45
  • 4
    Probably relevant: [Why is TypeScript not warning when setting a non-nullable value as null?](https://stackoverflow.com/q/60889805) - if you enable `strictNullChecks` or better yet - `strict`, then TS will give you an error. – VLAZ Sep 13 '22 at 08:25
  • 1
    This already throws a TypeScript error: https://tsplay.dev/wRlgxm – Tobias S. Sep 13 '22 at 08:26
  • 1
    @TobiasS. unless strictNullChecks is disabed: https://tsplay.dev/NVkvGm – VLAZ Sep 13 '22 at 08:27
  • 1
    You shouldn't use TSLint. It was deprecated in 2019 and is not updated or maintained for one or two years.. – jabaa Sep 13 '22 at 08:38
  • Thanks, guys! I had some weird stuff going in my typescript linter settings – Mikhail Macherkevich Oct 05 '22 at 18:40

0 Answers0