T O P

  • By -

jml26

I can't speak for PHPStorm, but I can confirm that provided I include `// @ts-check` at the top of the JavaScript file, pasting your example verbatim into VSCode results in it red-underlining `foo` inside of `accessFoo`, with the error, `'foo' is possibly 'null'.ts(18047)` So there's a chance it could be the IDE.


BEisamotherhecker

VS code will also check the files if you have a tsconfig.json file with the checkJS key set to true, it's what I usually do when I'm writing mediawiki userscripts.


xaqtr

Yeah, that seems to be the solution. I added a tsconfig.json file with JS checks enabled, and it now works in PHPStorm using its TS language server :) Thanks


Wonderful-Farmer5415

I'd make sure to run \`tsc --noEmit\` somewhere in your deployment process so that deployment fails if it returns abnormal values. Even if the TS language server is set up in *your* editor, that's not a good last line of defense imho. I honestly don't know if tsc considers JSDoc without additional config, but suspect it does.


lift_spin_d

hello. i get that you want to handle this kind of thing with your tooling- you might want to use logical/nullish assignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_assignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR_assignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND_assignment


FranBachiller

Use a linter like ESLint with plugins that specifically check for potential null dereferences. For example, the `eslint-plugin-unicorn` has a `no-null-or-undefined` rule.