6

Can Lightning be written as TypeScript? Or will Salesforce ever wrap their Lightning Javascript in TypeScript?

I know that you can have TypeScript in Salesforce per the video Making Visualforce RemoteAction Bulletproof With TypeScript.

Aura Lightning feels conceptually like Angular and Angular is in TypeScript. I wonder if Lightning could be made safer in TypeScript.

6

Aura does not support TypeScript, so for the time being, you'll have to live with what we're given, which is Locker Service. Note that the Issue logged suggests that TypeScript might be something in the future, but you'll probably want to log an idea.

Note that Locker Service, while it doesn't necessarily provide the same language benefits of TypeScript, still ensures a very high level of security, including protecting the native DOM elements, preventing components from talking to each other in unapproved manners, secure resources, and so on. It's kind of "functionally" the same, but definitely not the same.

5
  • Thanks for sharing the Locker Service! I never knew that existed...
    – akcorp2003
    Dec 2 '16 at 16:50
  • Typescript just transpiles down to javascript so why wouldn't it work?
    – NSjonas
    Apr 11 '18 at 4:50
  • @NSjonas Because there's no TypeScript transpiler built in to Aura as of yet. This means that you'd have to write it in TypeScript, transpile to JavaScript, then move that source over in to your Aura Bundles. Externally speaking, you can use TypeScript, but you can't send it in directly as an AuraBundleDefinition; until we type TypeScript directly into a source file (e.g. Developer Console), it can't be said to be supported.
    – sfdcfox
    Apr 11 '18 at 5:15
  • really that's sounds like the typically workflow for typescript anyways. As far as I'm aware, Typescript doesn't execute natively on any platform/framework and always requires a transpile step to dist files. Doing it in the developer console would not be possible but you could easily have a build process in which you could have your ts files mapped to "AuraBundleDefinition". I may look up a lightning example app and put together an example of how this could be done I can't image they would ever support in
    – NSjonas
    Apr 11 '18 at 5:36
  • 1
    @NSjonas They could; I believe they're currently using some variant of Closure for compilation as it is right now.
    – sfdcfox
    Apr 11 '18 at 5:38
0

While it's not natively supported (highly doubt it ever will be), you can use TypeScript in a Lightning component.

As an example, I ported the Belgium Beer app to a TypeScript project. It's actually really simple:

  • Add a tsconfig.json file
  • Add some very simple build scripts to transpile the ts files and merge them back in with all the other files.

However, since there is not yet typings for the Aura API, the benefits are limited to your own code.

Just for an example, I added a few of the API's methods to the root types.d.ts and I typed some of the BeerList Component.

NOTE: I've only tested this app. Certain TypeScript features may not transpile into JavaScript that Salesforce accepts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.