-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TypeScript support to LWC authoring format #2078
Comments
+1 I would love TypeScript support, but I can understand how this might be difficult on the platform because of the non-standard import scheme used before compilation. |
For the record, LWC does allow Typescript, here is an example (it might be a bit outdated but will be good enough for making the point): In a nutshell you just need to remove the types before running LWC compiler. As for the platform usage: We will never be able to natively support Typescript (meaning we won't be able to ever allow you to save components in original .ts format) fundamentally due to the fact that typescript can have breaking changes from version to version, which means that we will have to support N+1 versions of platform until the ends of time, which is something we can't afford to do. We do want to provide types and tooling to make it as seamless as possible, so all developers can choose to use Typescript from a "practically full" e2e development in platform. |
@diervo thank you. I googled it and could not find anywhere. |
Yes, that's the goal. I would suggest you create an item in the IdeaExchange https://trailblazer.salesforce.com/ideaSearch (if there is not one already) |
Even if LWC on platform doesn't "natively" run TypeScript, tooling could make it a near-native solution:
Here's the most applicable idea to upvote: https://ideas.salesforce.com/s/idea/a0B8W00000GdZyWUAV/enable-lightning-component-developers-to-use-typescript-and-ecmascript-6 |
This is still planned |
Where can I provide feedback for this implementation? https://developer.salesforce.com/docs/platform/lwc/guide/ts.html#compile-your-typescript-components-to-javascript I'm trying to use decorators and it's not clear how they are supposed to be handled. It works if I enable |
What version of TypeScript are you using? LWC currently uses non-experimental decorators, so you must be using v5 or later. You also must have For some tips on adopting TypeScript, you can see the LWC v7 release notes. |
@wjhsf I have the exact same problem, had a quick chat with Clay Martin about it, but wasn't able to make it work yet. I've added ESNext too (which is not in the doc here), but that didn't helped. Setting Even with a basic component using a decorator (
I'm getting the following error with Beside that, with this expected setup:
and with the same component but without the
Deploying this to the org (Winter '25 Scratch Org, LWC and sfdx-project.json set to API 62.0), adding the component to a FlexiPage crashes with the following error:
We are several trying to use it and so far I've seen no one succeeding. |
That's just the result of the TypeScript compilation step, correct? You'll need to update your |
Yes correct. According to the diagram on this page I understand that after the TypeScript compilation step I can just deploy the compiled JS to the org. I already tried to add
Is it what you're talking about ? I also tried to had |
@wjhsf ok it works with this config 🙂:
I think the The TypeScript error I reported before is still there, but it doesn't prevent the file from being generated. |
Yep, updated docs should be available soon. I think the next scheduled update is Tuesday.
Yep, that's the default behavior of TypeScript. If you want errors to prevent emitting JavaScript, you can set the compiler option |
Awesome, the |
relates: forcedotcom/cli#3032 |
If you're using the Salesforce VS Code extension, then If you're not using the VS Code extension, ensure your project is using LWC v7 or later. |
Hi, @wjhsf. Semi-related, is there already an open issue here for making this work better with third-party tooling, i.e,. not having a concrete dependency on the contents of I've already heard that that's tracked internally at Salesforce, but is there an outward-facing issue in some issue tracker -- this one or otherwise -- that I could use to track progress on third-party tooling enablement? |
I don't know that we have an external issue for removing all of the Longer term, the goal is to entirely replace the extension's types with the @salesforce/lightning-types npm package, which will work with any toolchain. We're currently working on populating the package (which will eventually have types for ~200 components/modules!). As we add type definitions that conflict with the extension's types, we will remove them from the extension. |
Thank you @wjhsf ! I'll live with that in the dev preview, but there may be some issues in VS Code until we get to the correct LWC version. Basically, just for information, for a component being:
with this
I'm getting the decorator error, but if don't enable
If I only change
|
It's admittedly less than ideal, but one thing you can do to paper over the issue is add |
One issue I'm also seeing is that the default ESLint rules don't seem to like TypeScript very much. They yield a nice, generic Has anyone resolved this short of just disabling ESLint in TypeScript-based projects? Is this something that will be rolled back into the CLI project template's |
What gets installed with a CLI-created project? And can you elaborate more on the conflicts? Is it just breaking changes to rules between versions of typescript-eslint, or is it something else? Something to be aware of is that eslint-plugin-lwc was written using the babel parser. Because eslint only supports one parser per file, you will end up with some rules not working correctly. Most of the incompatibility is in code involving TypeScript features, because the parsers build different ASTs (their ASTs are mostly compatible for JS features). I've only seen false negatives occur, no false positives, so you shouldn't have to deal with any invalid errors. One mitigation for this is to run eslint in two passes, one with each parser. I'm also hoping to eventually add typescript-eslint support to the plugin (salesforce/eslint-plugin-lwc#153). |
@wjhsf, I'm able to reproduce this very easily as follows:
Edit
Edit import { api, LightningElement } from 'lwc';
export default class TsComponent extends LightningElement {
@api myProperty: string;
} Run ESLint and it fails as noted previously due to a presumptive lack of TypeScript config:
If you then try to add the aforementioned dependencies, you get the following failures:
and:
Let me know if that doesn't provide a sufficient level of detail around this issue. |
Short version: Upgrade to Long version: Problem
I'm not 100% sure of what's going on with npm in those last two steps, but that's what seems to be happening. Solution Any of these actions will fix the issue:
I'll also follow up internally to get generated projects using the latest |
Thanks so much for your in-depth analysis of this issue and the potential solutions/workaround. With your recommendation plus this config info: https://typescript-eslint.io/getting-started/ I've been able to get it going nicely. Again, much appreciated, and anything you can do to help make sure this is on the appropriate teams' lists. |
I've verified that you can now install the |
Is your feature request related to a problem? Please describe.
No
Describe the solution you'd like
Add typescript support
Describe alternatives you've considered
NA
Additional context
The industry of frontend frameworks are going towards Typescript, Thinking on that, will LWC support typescript in the near future?
The text was updated successfully, but these errors were encountered: