typescript fundamentals

30
Fundamentals C# Corner – Delhi Chapter Sunny Sharma

Upload: sunny-sharma

Post on 20-Jan-2017

298 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Typescript Fundamentals

Fundamentals

C# Corner – Delhi Chapter Sunny Sharma

Page 2: Typescript Fundamentals

• Typed SuperSet of JavaScript

• Any Existing JavaScript Script/ program is also a valid TypeScript

• Adds typing

• No Special Runtime

• Transpiles to JavaScript

TypeScript

Page 3: Typescript Fundamentals

• Early detection of Bugs

• Auto-Complete/ Intellisense

• Adds typing

• No Special Runtime

• Transpiles to JavaScript

Benefits of a Typed Language

Page 4: Typescript Fundamentals

TypeScript-Features

• Type Annotations• Public / Private Attributes• Compile Type Checking• Type inference• Access Modifiers• Interfaces• Inheritance

• Enums• Optional properties•Modules & Namespaces• Tuple Types•Mixin• Generics

www.typescriptlang.org

Page 5: Typescript Fundamentals

The more familiar you are with the language,

The easier it is to understand

Page 6: Typescript Fundamentals

Data Types

• boolean• string• number

• enum• array• interface• class• any• Function

Built-in Custom

Page 7: Typescript Fundamentals

compile-time type checking leads to fewer bugs!

Page 8: Typescript Fundamentals

You will love TypeScript if:• Love C# or Java and find JS quite vague

• You mostly use Visual Studio or VS Code for Refactoring Support

• You like when you catch lot of your bugs by Type-Checking

Page 9: Typescript Fundamentals

How TypeScript Works?

• TypeScript File: main.ts• > tsc main.ts main.js• tsc main.ts -sourcemap main.js + main.js.map

Map files are used to debug TypeScript files instead of JavaScript directly.

Page 10: Typescript Fundamentals

TypeScript JavaScript

Page 11: Typescript Fundamentals

Setup & Installation

Page 12: Typescript Fundamentals

• Uses Node.js to run typescript compiler• Supports a number of IDE / Editors:

Page 13: Typescript Fundamentals

install typescript using npm

Page 14: Typescript Fundamentals

Type Inference

Page 15: Typescript Fundamentals

Access Modifiers

Page 16: Typescript Fundamentals

Properties

Page 17: Typescript Fundamentals

Arrow Functions

Shorter

Page 18: Typescript Fundamentals

Enums

Page 19: Typescript Fundamentals

Type Definition• Helps TypeScript getting you the Intellisense.

Page 20: Typescript Fundamentals

Interface

Page 21: Typescript Fundamentals

Class

Page 22: Typescript Fundamentals

Inheritance

Page 23: Typescript Fundamentals

Two types of Encapsulation Containers

• Modules• Namespaces

Page 24: Typescript Fundamentals

Modules• Way to Group Code• The file is the Container• Other files have to import• No reference path needed

• TypeScript relies on a Module Loader to load the modules.

• For bigger projects you need an External Module Loader.• Loaders: RequireJS, CommonJS, SystemJS … etc.

Page 25: Typescript Fundamentals

Modules

Page 26: Typescript Fundamentals

Namespaces• By default every object is added to the Global Namespace• A way to group classes.• Way to reduce confliction.

Page 27: Typescript Fundamentals

Namespaces

Page 28: Typescript Fundamentals

Modules vs Namespaces

• Tool for organizing code• Native Support in Node.js• Browsers supported with Module

Loaders• Code re-use

• Tool for organizing code• No special loader needed• Prevents Global namespace

Pollution• Best for small applications

Modules Namespaces

Module wins the verdict for bigger project

Page 29: Typescript Fundamentals

Questions?

Page 30: Typescript Fundamentals

Thank You!