2

I am attempting to enable TypeScript within my Lightning Web Components but running into some issues.

In jsconfig.json, I've added the following:

"compilerOptions": {
  "experimentalDecorators": true,
  "checkJs": true,
  "target": "ESNext"
},

This mostly works, but there are some errors that aren't clear to me how they should be resolved. In a component's JS file, here are some of the errors:

myComponent.js:

import { getSObjectValue } from "@salesforce/apex";
import ASSET_OBJECT from "@salesforce/schema/Asset";

Module '"@salesforce/apex"' has no exported member 'getSObjectValue'.ts(2305)

Cannot find module '@salesforce/schema/Asset'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?ts(2792)

In some documentation I've seen, it's recommended to explicitly define the Objects using TypeScript annotations. If that's the case, then what is the point of also importing the Object from the Schema?

Salesforce Documentation:

1 Answer 1

0

You can solve those issues by adding those types manually in a type definition file (a .d.ts file) in your project. You could keep them local in a specific folder or make it globally accessible in your project, just remember to add it to the jsconfig.json file.

I came across this post with the same issue, but with the ShowToastEvent from the lightning/platformShowToastEvent module. This snippet defines this type and makes that error go away:

declare module "lightning/platformShowToastEvent" {
  class ShowToastEvent extends Event {
    constructor(params: object);
  }
}

For this snippet, I created a global generics.d.ts file at the root of my project. The jsconfig.json definition in my lwc folder has its include attribute like this:

"include": [
    "**/*",
    "../../../../.sfdx/typings/lwc/**/*.d.ts",
    "../../../../*.d.ts"
]

The third value is the string that specifies "import every definition file that is on the root too". If you have multiple jsconfig.json files in your project, you might need to add this line in all of them.

You must log in to answer this question.

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