TypeScript

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


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.







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