Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

TypeScript Tutorial

TypeScript Tutorial

TypeScript is a superset of JavaScript that supports the ECMAScript 6 standard (ES6 Tutorial)。

TypeScript is a free and open-source programming language developed by Microsoft; it is a superset that adds static type checking on top of JavaScript.

TypeScript is designed for developing large applications; it can be compiled into pure JavaScript, and the resulting JavaScript can run in any browser.


Why Choose TypeScript

TypeScript is an open-source programming language developed by Microsoft; it is a superset of JavaScript.

TypeScript greatly enhances the JavaScript development experience by adding features such as optional static types, interfaces, and enums.

Key Advantages of TypeScript

How TypeScript Works

TypeScript cannot run directly in a browser; it must first be compiled to JavaScript. This compilation process checks for type errors and converts TypeScript-specific syntax into pure JavaScript.

.ts file TypeScript Compiler (tsc) .js file Browser/Node.js ✓

The TypeScript compiler (tsc) performs type checking during compilation; if it finds type errors, it reports them and blocks compilation. After successful compilation, it generates pure JavaScript code that can run in any browser or Node.js environment.


Language Characteristics

TypeScript is a type-enhanced extension of JavaScript that, while maintaining compatibility with the original syntax, introduces a stronger type system and engineering capabilities.

Core Enhanced Capabilities (Unique to TypeScript)

Feature Description
Type annotations & compile-time checking Detect type errors during development, improving code reliability
Type Inference No need to explicitly declare types; the compiler can infer them automatically
Type erasure Remove type information after compilation, outputting pure JavaScript
Interface Used to define object structures and enhance code constraints
Enum Provide a collection of named constants, improving readability
Generics Support type reuse, enhancing the flexibility of functions and classes
Mixin Implement a composition pattern for multiple inheritance
Namespace Organize code structure (an older approach; modules are now more commonly used)
Tuple An array with a fixed length and different types
Async (Await) Provide more intuitive asynchronous flow control

Syntax support from ES2015 (backward compatible)

TypeScript simultaneously supports and compiles down the core features of ECMAScript 2015:

Feature Description
Class Object-oriented programming support
Module Support modular development (import/export)
Arrow function (=>) A more concise function syntax that binds lexical scope
Optional arguments Function parameters can be omitted
Default arguments Provide default values for parameters

The Difference Between JavaScript and TypeScript

TypeScript is a superset of JavaScript that extends its syntax, so existing JavaScript code can work with TypeScript without any modifications. TypeScript provides compile-time static type checking through type annotations.

TypeScript can handle existing JavaScript code and compile only the TypeScript code within it.


The First TypeScript Example

In the following example, we use TypeScript to output Hello World!:

Example

const

hello

:

string

=

"

Hello World!

"

console

.

log

(

hello

)

Try it »