TypeScript

Launched in 2012 and developed by Microsoft as Open Source
Strongly Typed Superset of JavaScript TypeScript = JavaScript + Type Definitions
TypeScript (like C#) is not an interpreted language, it must be compiled first TypeScript compiles down to normal JavaScript (ECMAScript 2009 / ES5)
Features in TypeScript are being incorporated into future releases of JavaScript
Visual Studio 2017 (15.9) supports TypeScript 3.1 TypeScript fully supports JSX


TypeScript provides the ability to add typed variables to your JavaScript code


TypeScript lets you add type information to variables by appending a colon and a type name after the declaration.
www.typescriptlang.org/index.html


Arrow Functions

Arrow Functions added to TypeScript 1.0 (released April 2014)
Arrow Functions added to JavaScript / ECMAScript 2015 (released June 2015)



Class

Classes added to TypeScript 1.0 (released April 2014)
Classes added to JavaScript / ECMAScript 2015 (released June 2015)



Let Keyword

When variables are declared inside loops both VBA and C# limit the scope and usability of that variable to just that loop.
This is not the case in JavaScript when you use 'var'.

var selectionRange = context.workbook.getSelectedRange() 

TypeScript offers a keyword 'let' which can be used in place of 'var'.
You can achieve this behaviour in TypeScript using the 'let' keyword to introduce better scoping rules.

let selectionRange = context.workbook.getSelectedRange() 


Type Information

TypeScript lets you add type information to variables.
This can be doen by appending a colon and a type after the declaration.

let myNumber : number; 
let myString : string;
let myBoolean : boolean;

let myArray1 : number[];
let myArray2 : Array<number>;

let myObject : Excel.Range;


Function Return Type Declarations

This way the variable will get implicitly typed.

function getSomething() : number { } 
let myNumber = getSomething();


2D Array Types

These return types are always 2D arrays.
If you need to assign a single value you can prefix your value with '<any>'

myExcelRange.values = <any> 20; 
myExcelRange.formulas = <any> "=4";


Await / Async

JavaScript Line

Excel.run(function (context) { 
   return context.sync()
      .then(function () {
         // add code
      }
      .then(context.sync();

TypeScript equivalent

   await context.sync() 
   // add code
   await context.sync()


Null and Undefined

The && operator adds null and/or undefined to the type of the right operand depending on which are present in the type of the left operand.


The || operator removes both null and undefined from the type of the left operand in a resulting union.



© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopNext