Table of Contents

[Pages:145] TypeScript Deep Dive

Table of Contents

Introduction Gettting Started

Why TypeScript Future JavaScript Now

Classes Classes Emit Classes Super Classes Extensibility

Arrow Functions Rest Parameters let const Destructuring for...of Template Strings Spread Operator Enums Generators Async Await Project Compilation Context

tsconfig.json Declaration Spaces Modules

File Module Details globals.d.ts Namespaces TypeScript's Type System JS Migration Guide Ambient Declarations

0 1 1.1 2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 3 3.1 3.1.1 3.2 3.3 3.3.1 3.3.2 3.4 4 4.1 4.2

2

TypeScript Deep Dive

Declaration Files Variables Interfaces `lib.d.ts` Functions Type Assertion Freshness NodeJS JSX TIPs Quick Object Return String Based Enums Nominal Typing Stateful Functions TypeScript Compiler Internals Program AST TIP: Visit Children TIP: SyntaxKind enum Trivia Scanner Parser Parser Functions Binder Binder Functions Binder Declarations Binder Container Binder SymbolTable Binder Error Reporting Checker Checker Diagnostics Checker Error Reporting Emitter Emitter Functions

4.2.1 4.2.2 4.2.3

4.3 4.4 4.5 4.6

5 6 7 7.1 7.2 7.3 7.4 8 8.1 8.2 8.2.1 8.2.2 8.2.3 8.3 8.4 8.4.1 8.5 8.5.1 8.5.2 8.5.3 8.5.4 8.5.5 8.6 8.6.1 8.6.2 8.7 8.7.1

3

TypeScript Deep Dive

Emitter SourceMaps Contributing Glossary

8.7.2 8.8

4

TypeScript Deep Dive

TypeScript Deep Dive

I've been looking at the issues that turn up commonly when people start using TypeScript. This is based on the lessons from StackOverflow / DefinitelyTyped and general engagement with the TypeScript community. You can follow for updates. If you are here to read the book get started. You can also do one of the following:

EPUB for iPad,iPhone,Mac PDF for Windows and others MOBI for Kindle Share URL:

Introduction

5

TypeScript Deep Dive

Getting Started With TypeScript

TypeScript compiles into JavaScript. JavaScript is what you are actually going to execute (either in the browser or on the server). So you are going to need the following:

TypeScript compiler (OSS available in source and on NPM) A TypeScript editor (traditionally visual studio) Some build pipeline for a build server Traditionally you would need to set all these up (and more) but we're trying to consolidate all this into a single Atom Package : Atom-TypeScript. The only thing you need to share and collaborate on TypeScript projects across platforms (Windows / Mac / Linux).

So: 1. Install atom. 2. apm install atom-typescript 3. Fire up atom. Wait (around 5 mins) for the message: AtomTS: Dependencies installed

correctly. Enjoy TypeScript

Now create a new .ts TypeScript file and start hacking away. AtomTS will take care of compiling it to .js and create a default tsconfig.json TypeScript project file for you.

Getting the Source Code

The source for this book is available in the books github repository most of the code samples can be copied in to atom-typescript and run as is. For code samples that need additional setup

Gettting Started

6

TypeScript Deep Dive

(e.g. npm modules), we will link you to the code sample before presenting the code. e.g.

this/will/be/the/link/to/the/code.ts // This will be the code under discussion

Nightly TypeScript

Instead of using the official stable TypeScript compiler we will be presenting a lot of new stuff in this book that may not be released. For this purpose we recommend using nightly typescript versions.

npm install -g typescript@next

TypeScript definitions

TypeScript has a concept of a declaration file for external JavaScript code bases. High quality files exist for nearly 90% of the top JavaScript libraries out there in a project called DefinitelyTyped. You will need tsd to get these defintions. Don't worry, we will explain what this means later ... just install for now:

npm install -g tsd

With a dev setup out of the way lets jump into TypeScript syntax.

Gettting Started

7

TypeScript Deep Dive

Why TypeScript

There are two main goals of TypeScript: Provide an optional type system for JavaScript. Provide planned features from future JavaScript editions to current JavaScript engines

The desire for these goals is motivated below.

The TypeScript type system

You might be wondering "Why add types to JavaScript?" Types have proven ability to enhance code quality and understandability. Large teams (google,microsoft,facebook) have continually arrived at this conclusion. Specifically:

Types increase your agility when doing refactoring. Its better for the compiler to catch errors than to have things fail at runtime. Types are one of the best forms of documentation you can have. The function signature is a theorem and the function body is the proof. However types have a way of being unnecessarily ceremonious. TypeScript is very particular about keeping the barrier to entry as low as possible. Here's how:

Your JavaScript is TypeScript

TypeScript provides compile time type safety for your JavaScript code. This is no surprise given its name. The great thing is that the types are completely optional. Your JavaScript code .js file can be renamed to a .ts file and TypeScript will still give you back valid .js equivalent to the original JavaScript file. TypeScript is intentionally and strictly a superset of JavaScript with optional Type checking.

Types can be Implicit

TypeScript will try to infer as much of the type information as it can in order to give you type safety with minimal cost of productivity during code development. For example, in the following example TypeScript will know that foo is of type number below and will give an error on the second line as shown:

Why TypeScript

8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download