Typescript Flashcards
TypeScript
TypeScript extends JavaScript by adding types to the language.
TypeScript is an open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. TypeScript is designed for development of large applications and transcompiles to JavaScript.
TypeScript vs JavaScript
TypeScript = JavaScript + Types + other stuffs
Types: type checking, type definition,…
TypeScript is superset of JavaScript
Why TypeScript?
Makes building these complex apps more manageable
Framework like angular 2 using it
Why need Node.js in Typescript
Node.js compile Typescript to Javascript
Run Javascript on Node.js
Typescript array
var myarr : number[]; myarr =[]; myarr=[1,2]; myarr.push(1); myarr.push(''); // error var a:number = myarr.pop();
If there is an error in TypeScript Compiler, will compiled js file be created?
Yes.
The error is only used for
TypeScript vs JavaScript in action
- Type restrict in TypeScript
- In JS, there is no restriction in function types or number of arguments but there is in TypeScript
TypeScript function
- Can use JS to define a function
- Restrict number of augments input and augment types
Optional augments in TypeScript function
function add (a, b, c=0) { return a + b + c; }
function add (a, b, c?) { return a + b + c; }
function add (a, b?, c) { // not valid return a + b + c; }
Implicit typing
var a = 10; // TS automatically apply type number to a var b = true; // TS automatically apply type boolean to a var c = "Hello"; // TS automatically apply type string to a
Implicit typing
var a = 10; // TS automatically apply type number to a var b = true; // TS automatically apply type boolean to b var c = "Hello"; // TS automatically apply type string to c
function greeting() : string { return "Hello"; }
var hello = greeting(); // TS automatically apply type string to hello
Notes:
- Has to be on the same line variable and assigned value
Type any in TS
var a;
var a: any;
a = 10; a = true; a = "String";
-> Use to migrate JS to TS
Union Type
var a : boolean | number;
a = 10; a = true; a = "String"; --> Error