3

I'm experimenting with using TypeScript to the extent possible in Lightning Web Components (as described in this article), and while it works if I put // @ts-check at the top of my JavaScript file, removing this line and including "checkJs": true in tsconfig.json removes the error reporting that TypeScript gives when the check line is the first line of the file.

How can I avoid having to put // @ts-check at the top of all my JS files? Do I need to specify a path for TypeScript to find the JavaScript files? All our components are found in force-app/core/lwc/, although if we decide to use TypeScript across our projects, we would have additional lwc folders (all within force-app).

I did find this GitHub issue, where a reply says:

And thus --checkJs really means nothing without --allowJs.

So I tried enabling that as well with no change in results. Errors are still not reported without // @ts-check in the first line.

1 Answer 1

1

As per this same article , we need to specify target compiler in jsconfig.json file of lwc folder.

Below is the added config :

"compilerOptions": {
        "experimentalDecorators": true,
        "checkJs": true,
        "target": "ESNext"
    },
    "include": [
        "**/*",
        "../../../../.sfdx/typings/lwc/**/*.d.ts"
    ],

The blog also states to add //@ts-nocheck for skipping type checking :

With the checkJs flag added, you don’t need to add //@ts-check to each file anymore. Instead, to skip type checking on specific files, add the //@ts-nocheck flag at the beginning of those files.

Above changes worked for me. Let me know if it helps.

Edit : tsconfig.json is required for LWR (Lightning Web Runtime) apps. For LWC , we need to define configs in jsconfig.json

1
  • 1
    My jsconfig.json already had the paths included, but the compilerOptions didn't include the checkJs property. Adding that here, instead of to tsconfig.json, seems to have fixed the problem.
    – Chuck
    Commented Aug 7, 2023 at 18:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .