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
link - channel9.msdn.com/Shows/Web-Wednesday/What-is-TypeScript
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


.TS and .TSX

A TS file is a text file that contains TypeScript code, an open source programming language developed by Microsoft.
TypeScript is a superset of ECMAScript 2015, which is a superset of JavaScript.
It is used for developing medium- to large-scale JavaScript applications for server-side or client-side execution.
TS files are similar to JavaScript .JS files.
To use JSX with React in your TypeScript, use the *.tsx file extension instead of the normal *.ts



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.



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