Is TypeScript worth the bother for SharePoint JavaScript snippets?

Introduction

With the SharePoint Framework and other JavaScript Frameworks (React, Angular, VueJS, Aurelia) all the rage, and Typescript (and Babel) bringing C#-like capabilities to JavaScript … does it still make sense to write “raw JavaScript” (ES5) for SharePoint customizations? You know what I’m talking about…  those small snippets of JavaScript you’ve embedded within Script Web Parts, or linked to from Content Editor Web Parts, or injected into a SharePoint page via JSLink. You’ve been following the latest technical advancements with tooling, keeping abreast of TypeScript and ECMAScript advancements. Perhaps taken some online training. But still, you’ve hung back .. stayed in your comfort zone with JavaScript. And you’ve wondered… do the benefits of TypeScript – with its static type checking, ES2015 language enhancements, and advanced language features like async/await – outweigh the hassle of setting up a “build” process for transpilation, bundling and minification – for relatively small amounts of JavaScript?

(Note…  if you’ve been out of JavaScript development for a year, then this article is required reading before proceeding further.  Special thanks to Eric Maffei for bringing it to my attention)

I decided to investigate. And for a test case, I chose an example from an actual project, but simplified for this article. The JavaScript example I chose uses Client Side Rendering (CSR) to provide “field-level permissions” for items in a custom list.  CSR was introduced by Microsoft a few years ago as a way to control – via client side JavaScript – the rendering of list views and list item forms.  I’ve found CSR especially useful for manipulating field controls on the New/Edit/View forms of SharePoint custom lists.  Essentially, CSR allows for custom “field-level” permissions.

(I often hear “CSR” confused with “JSLink”. They are not the same.  “CSR” is a Microsoft-supplied SharePoint framework for allowing developers to customize fields within lists and forms. “JSLink” refers to a new string property exposed in list and form web parts to allow CSR JavaScript files to be injected into the page that is rendering the list or form.)

The chosen example contains almost 300 lines of ES5 code, and uses two popular JavaScript libraries – jQuery (for DOM manipulation and Ajax calls) and KnockOutJS (for field validation and display). After converting the example to TypeScript, using several TypeScript-specific language features, experimenting with various “build process” tool chains, and examining the debugging experience, what is my conclusion?

TypeScript is worth the effort (for 300 lines of code definitely… 30 lines of code… maybe not).  Especially if you’re starting from scratch. Converting the JavaScript to TypeScript was easy, and went quickly. And by “convert”, I mean using classes and type annotations and namespaces and a few other TypeScript features. During the conversion, I half-expected to see lots of TypeScript bugs, but that didn’t happen – probably because the JavaScript had already been thoroughly tested and debugged (that was a first…. being disappointed to not see bugs). But in fairness, TypeScript didn’t drastically simplify the code, or reduce the number of lines of code. With only 300 lines of (dare I say) well-crafted code to start with, TypeScript language improvements could only go so far. So, you ask, why my favorable conclusion?

For one thing, TypeScript eliminated several traditional JavaScript “gotchas” that tend to blow-up at runtime. You know the usual suspects – bugs arising from JavaScript hoisting “var” declarations you placed in a block deep down inside a function, and bugs from using “==” instead of “===”. TypeScript can eliminate them easily (for example, using let instead of var eliminates hoisting bugs). Also, TypeScript helps clarify the code. TypeScript type annotations –  while providing the wonderful benefit of “transpile-time” type checking – improves code “readability” by making variables, function parameters and function return types explicit.  So the developer maintaining your code immediately knows your intentions rather than having to infer it. Additionally, TypeScript classes provide that “Object-Oriented” expressiveness I sorely missed with JavaScript. Interestingly, the latest version of TypeScript (2.2.2 as I write this) boasts the ability to transpile asynch/await statements to ES5 (previous versions were restricted to ES2015 and above), and using asynch/await did simplify my example’s ajax calls somewhat – but not enough to declare them a “must use” feature.

Ok…  so TypeScript offers these nice features (and many more I haven’t touched on here). But what about the hassle of creating a “tool chain” for transpiling, bundling and minifying that TypeScript code?

To my surprise, establishing a tool chain wasn’t too much hassle. It’s largely a “one-time cost” in the initial setup… knowing which node.js npm packages to assemble and configure. But once that’s figured out… the tool chain is easily re-used on other projects.  During my investigation, I used two popular “tool chains” – Gulp with Browserify, and webpack. (Spoiler alert… I found webpack to be the better approach).

So yes, I think TypeScript is worth the effort even for small amounts of JavaScript code – for the likely chance it will catch bugs earlier in development, improve code readability, and allow for more elegant code abstractions.  For larger amounts of code, TypeScript is a no-brainer.  If only because of the immense advantages of using ES2015 modules to break the code into manageable pieces (demonstrated in this article – admittedly somewhat artificially – by importing jQuery and KnockOut).

If you’re interested in the details of my investigation, please read on. We’ll take a quick look at the example EditForm.aspx page I “modified” with the CSR script, look at some of the TypeScript conversions used, discuss my experience with the two tool chains and debugging experience, and conclude with some final observations.

The Clean Team Form example

Here’s some context about our example.

The ACME Crime Labs corporation uses SharePoint Online to collaborate on criminal investigations. For each investigation, a custom site is provisioned, containing a custom list – CTMembers – for detectives and aides of a “Clean Team” crime unit (“Clean” means the members have no conflicts of interest to the crime investigation). Clean Team members can request access to a secure data room containing highly confidential documents. In our example, we have a CTMembers list with two items… I mean, two detectives – Robert and duke:

fig01

The list has a “Person” column and a “Yes/No” column for Data Room Access:

fig02

Clean Team members can only edit their own “Needs Data Room Access” setting (I said this example was simplified), unless they are site “Owners”. Owners can edit anyone.

Robert is a site “Owner”. Duke is not. Here is what they see when attempting to edit a member:

Duke cannot edit Robert (“Save” button and checkbox are disabled):
fig03a

Duke can edit himself:
fig03b

Robert can edit himself:
fig03c

Robert can edit Duke:
fig03d

Duke can check only his own checkbox, while Robert can check anyone’s – since Robert is a site owner and Duke isn’t. The “field-level” permissions that control this edit behavior is implemented with CSR in under 300 lines of JavaScript.  (If you want to learn more about CSR, here’s a nice example by my friend and colleague Javed Ansari.  For CSR fundamentals, go here ).

Getting the CSR script into the EditForm.aspx page (along with the jQuery and Knockout libraries) is easy. I put them in a Site Assets library within the site collection – and “inject them” into the page via the JSLink property in the EditForm.aspx web part tool pane:

fig03

To get all three JavaScript files injected via the JSLink property… I concatenated the URLs with a vertical separator character ‘|’, like this:

https://…/SiteAssets/CSR.js | https://…/SiteAssets/jQuery.js | https://…/SiteAssets/knockout.js

[I prefer SharePoint Designer to set the JSLink property – it’s easier than navigating the SharePoint UI].

My dev/test/deploy cycle is straightforward:  Edit the CSR JavaScript with VS Code, copy it to SharePoint, debug in Chrome.  Wash, rinse and repeat.

With only 300 lines of JavaScript code to deal with, this “application life cycle” is not terrible… nor is the debugging too bad (I’m particularly fond of Chrome Developer Tools for debugging). But it took many cycles to ensure the JavaScript code was solid.

Some Easy-Peezee TypeScript Refactoring

So what magic can TypeScript bring to this party? And what is the least amount of effort needed to configure the VS Code editor for TypeScript – no Gulp, no Browserify, no webpack. (Bowden Kelly, Program Manager for Visual Studio, gave an excellent talk  on this very topic). But I had to see for myself, so here’s what I did.

“No Tool Chain” VS Code Configuration for TypeScript
(Effort: 5 minutes)

There are tons of examples for how to configure VS Code for TypeScript with a “build” tool chain to transpile/bundle/minify.  I wanted none of it… just VS Code (on my PC) and TypeScript. Here are the steps:

  1. From the integrated terminal prompt within vscode:
    • Type “npm init” (use all the defaults) to create and initialize package.json.
    • Add the following tsconfig.json file to the folder:fig04a
  1. Add the JavaScript file “js” to the top folder and change the extension from “.js” to “.ts”.
  2. Type CTRL-SHIFT-B to compile (transpile) “ts”. The first time you do this, you’ll be prompted to configure a task runner:
    fig04b

Go ahead and click “Configure Task Runner”, and then click the menu option for “TypeScript – tsconfig.json” :

fig04c

This creates a tasks.json file.

  1. Type CTRL-SHIFT-B again – the task runner invokes tsc.exe (the TypeScript compiler) and transpiles “CTMemberEditFormCSR.ts“, outputting “CTMemberEditFormCSR.js” to the “dist” folder, and places the TypeScript compiler into watch-mode (I’m assuming much of this is familiar to you).

Here is a screenshot after the initial transpilation:

fig04d

Wow… look at those errors.  The “Cannot find name…” errors are because TypeScript doesn’t know about jQuery, KnockOutJS, and SharePoint (e.g. SP.js). So let’s fix that.

5. Add Type Declarations for External Libraries

To be clear… I don’t want to download and bundle jQuery and KnockOutJS with my code. Those libraries are already deployed to SharePoint (and “included” in the SharePoint page via JSLink). I just want their TypeScript declarations so VS Code can provide Intellisense for them. According to Marius Schulz (one of my favorite TypeScript bloggers):

In TypeScript 2.0, it has become significantly easier to acquire type information for JavaScript libraries. There’s no longer a need for additional tools such as typings or tsd. Instead, type declaration packages are directly available on npm.

So to obtain the JavaScript TypeScript declarations for these libraries, I typed these npm commands in the VS Code terminal window:

npm install –save @types/jQuery
npm install –save @types/knockout
npm install –save @types/sharepoint

This created a node_modules folder and downloaded the declaration files into it and updates the project.json file (you may need to restart vscode to see the changes). For an in-depth discussion on this “typings” goodness, see Marius’s excellent blog.

At this point, VS Code can transpile my one TypeScript file to ES5 JavaScript, and the resulting JavaScript code is identical to the TypeScript code, except that it has been stripped of all blank lines.

Here are the files for VS Code.

This setup took about 5 minutes, so not too annoying. And now I’m ready to investigate the pros and cons of TypeScript. There are 5 areas I specifically wanted to refactor in my example code:

  1. Replace IIFE with a namespace
  2. Replace var with let
  3. Remove implicit casts
  4. add type annotations
  5. replace object literals with static classes

So without further ado, here are the results:

Refactoring – Replacing the IIFE with a namespace (Effort: 20 seconds)

I replaced the JavaScript IFFE (Immediately Invoked Function Expression) – a hideous ES5 mechanism for keeping variables out of the global JavaScript namespace – with a respectable TypeScript “namespace”.

That is, I replaced this:

//  IIFE

(function () {
all code goes here...
 })();

with this:

namespace myCTMemberEditFormCSR
{
all code goes here...
 }

Refactoring – Replacing var with let (Effort:  20 seconds)

JavaScript variables declared with var are scoped to the nearest enclosing function – and not the nearest enclosing block, always leaving me with that sneaking suspicion a bug may be lurking in my ES5 code. I much prefer let, which scopes variables to the nearest enclosing block – like C# declarations. In fact, I see no reason to ever use var when let is available. So I replaced all occurrences of var with let in the example

Refactoring – Remove Implicit Cast (Effort: 5 seconds)

I had been relying on the implicit casting behavior of the ‘==’ in ES5 to logically compare two numbers.  Here is the expression I was using:

// using "==" instead of "===" to cast string to number
//
(myCTMemberEditForm.CTMemberId == _spPageContextInfo.userId);

I was relying on the implicit casting behavior of the double ‘==’ in ES5 – a known source of errors in ES5 if used without awareness of this behavior, and the reason for many “Best Practice” recommendations to avoid it in favor of the triple ‘===’, which doesn’t cast.  Typescript will have none of this implicit casting nonsense and require you be explicit.  Hence the need to tack toString() on the end of the _spPageContextInfo.userId  ‘number’ variable in order to work with TypeScript:

(myCTMemberEditForm.CTMemberId == _spPageContextInfo.userId.toString());

Refactoring – Replacing Object Literals with Static Classes
(Effort: 10 minutes)

I often use JavaScript object literals as singletons to organize my code. These are “sort of” analogous to C# static class instance singletons. They do a decent job of making the code easier to read by grouping functions and variables that logically belong together. In the example, you’ll notice two “object literal singletons” –
loggedInUser (representing the user who is editing the form) and myCTMemberEditForm (representing the form and its field controls.)

By refactoring them into TypeScript classes with static properties and methods… we get the additional benefits of static type checking. So let’s do that.

Here’s the original:

var loggedInUser = loggedInUser || {};
loggedInUser.IsCTMember = false;
loggedInUser.IsSiteOwner= false;
:

And here’s the refactored version:

class loggedInUser
{
static IsCTMember: boolean  = false;
static IsSiteOwner: boolean = false;
 :
}

By annotating each member variable with a type, my intentions are explicit. And VS Code warns if there is a type mismatch, with visual “squiggles” in the editor.

Babel – another popular transpiler – does not offer type annotations. Babelites tout this as a benefit… because Babel can determine types by analyzing the code. TypeScript also does this in the absence of type annotations.  And this can be helpful in certain instances –  like when it’s too painful to figure out the proper type for a complex JSON string (similar to using var in C# to handle LINQ results). But in general, I prefer TypeScript annotations to make my intentions explicit, for better readability. But that’s just my opinion.

One thing I noticed while refactoring the “object literal singletons” into classes is how I had sprinkled declarations throughout the code, rather than in one place. In retrospect, this makes the JavaScript harder for someone else to understand. TypeScript classes tighten this up, improving readability.

Refactoring – Annotating the CSR object literal (effort: 1 hour)

When using CSR, you create a CSR “Template Override” object literal and initialize it with various callback functions. I wanted to annotate the object, and the callbacks.  It took significant effort. For example, I changed this:

var overrideCtx= {};

To this:

let overrideCtx: SPClientTemplates.TemplateOverridesOptions = {};

It was difficult locating the correct annotations… especially for the nested types within overrideCtx. In particular, it took me awhile to figure out that the correct function parameter used for overrideCtx.OnPostRender is:

static OnPostRenderFunc (ctx:SPClientTemplates.RenderContext_Form)

I suppose if I were doing many CSR scripts, this would be less of a burden, but for a one-off… it was a chore achieving this level of Typescript perfection.

Debugging TypeScript

It is comforting to debug in TypeScript. If you’re familiar with debugging JavaScript in Chrome – it’s almost identical.  You set your breakpoints in a TS file (you can also add a “debugger” statement in TS, just as you would in JS, and the Chrome debugger will pause when it hits it.) and away you go.

To get this to work, I needed to do three things:

  1. Set “sourceMap” to true in the tsconfig.json file
    fig05a
  1. Copy the transpiled output (and source map) to SharePoint. In my example, I copied them here:
    fig05b
  2. Copy the original typescript file to SharePoint, with the same folder hierarchy used in VS Code. So here it is in SharePoint:

fig05c

Bring in the Chains

You are probably using a more sophisticated “build” process (“tool chain”) for client-side development than what I’ve demonstrated so far. But really, is that needed here? In my opinion, perhaps not needed, but recommended. The hassle of configuring a more advanced build process is mostly a “one time” cost assembling the right combination of node.js npm packages to perform transpilation, bundling and minification tasks. And orchestrating those tasks. Once you’ve figured that out…. the tooling is easily re-used in subsequent projects.

I investigated two of the most popular “tool chains” to compare the effort needed to set them up, and to offer my advice choosing one over the other.

If you’ve been following the changes with client side tooling, you’ll know change is a constant. Within the last 18 months, I’ve personally used Grunt, then Gulp, then npm scripts, and experienced the joys of ES2015 modules and how best to link modules together, starting with runtime (dynamic) linking with loaders like SystemJS and JSPM, before switching to build-time (static) linking with bundlers like Browserify, and more recently, webpack. I see no end to the churn.

I suppose a tool chain might be overkill for one TypeScript file since there’s nothing to bundle. But then again, it might be nice to package up everything –  the TypeScript file + libraries – into a single bundle.js file. Here’s the idea graphically:

bundling workflowThe “Browserify Tool Chain” required several npm packages, a simplified tsconfig.json file, and gulp to co-ordinate the transpilation, bundling and minification tasks. The three files that control it all are shown below (and familiar if you’ve used gulp before):

fig06a

To separate my TypeScript file from these “controlling” files, I placed the TypeScript file into a src folder.  Here is the new folder structure in vscode:

fig06b

I admit my “Browserify” configuration is not optimal…  but my objective was to judge the effort to configure a “traditional front-end build process”. And so, while it’s more complex than before… it didn’t take too long to throw this together. Type “gulp” at a terminal prompt… and watch gulp do its thing.

I was disappointed in minification, however (which is why I commented out the minification step on line 25 in gulpfile.js. And minification isn’t going to do much with 300 lines anyway). The Browserify “Uglifier” severely degraded the runtime debugging experience in Chrome….   I couldn’t set breakpoints on every line nor reliably single-step through the minified code. So I recommend not minifying your transpiled TypeScript code.  And sure, I could optimize my Browserify build process, but Browserify is – in my opinion – obsolete. I prefer webpack (webpack 2, actually).

Here are the files for the Browserify build.

Webpack is another popular tool for transpiling, bundling and minifying TypeScript. It’s been around for several years, but seems (to me) to have really taken off in the last year. No doubt webpack is already obsolete for some of you (rollup, anyone?), but I think webpack will remain very popular for the foreseeable future.

Some of the coolest webpack features have cool names – like “Hot Module Reloading” and “tree shaking”. Two benefits I particularly like are “no gulp needed” (self-explanatory), and “vendor code splitting” (webpack bundles can be split up – such that 3rd party libraries are bundled separately from application code).

I was disappointed to find debugging of webpack minified code remained problematic, so I omitted minification as before. A good work-around is to use “vendor code splitting” to separate jQuery & KnockOutJS from the TypeScript code, keeping the application code relatively small, while also providing a good debugging experience (maybe I’ll do that in another blog)

Here are the three files that control the “webpack 2 tool chain”:

fig07a

And here is the file structure in VS Code:

fig07b

Note the npm script (in package.json) named “build”. To kick-off the build, just type “npm run build” in a terminal window prompt.

Here are the files for the webpack build.

In Conclusion…

So is TypeScript worth it for relatively small amounts of JavaScript? For new snippets, yes… I think TypeScript is worth the effort (along with a re-usable build process). TypeScript will catch more bugs at “transpile time” (instead of at run-time). And TypeScript offers much better language abstractions to better model your application code, and the debugging experience (as long as you don’t minify it) is good. But it’s probably not worth the effort to convert existing snippets of JavaScript to TypeScript.

One area I didn’t cover in this blog… was how TypeScript might improve asynchronous REST calls with asynch/await. I’ll cover that in my next blog (sneak peek… it helped).  And I’ll also look at the new Microsoft PnP JavaScript library offerings – which do promise (pun intended) to greatly simplify the asynchronous REST calls (especially PUT).

Happy Coding.

3 thoughts on “Is TypeScript worth the bother for SharePoint JavaScript snippets?

  1. Very nice! I’m in the process of getting familiar with webpack myself, in the context of Angular 2 and SharePoint 2013, and it’s a little bit of a chore figuring out how to set up that configuration file to do everything that I want it to. Thanks for including the webpack config file! It’ll definitely help my understanding.

    Liked by 1 person

  2. Pingback: Was waiting for async/await worth it? | SharePoint for the Big Company

Leave a comment