TypeScript
Concept
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript wird während des Entwicklungsvorgangs mit Hilfe eines Transpilers (eine Sonderform eines Compilers) in JavaScript übersetzt. The concepts and features below are add-ons to the JavaScript language.
Language
Variable Declaration
- see also here
- ? means optional, can be undefined
Type Annotations
funcXYZ : <RETURN_TYPE> {
}
varXYZ : <TYPE> = ...;
Classes
- constructor short cut: if access modifiers are used no need to declare properties
export class Menu {
constructor(
public id: number,
public parent_id: number,
public name: string,
public comment: string
) {
Interfaces
Typescript uses in interface in multiple ways (s. here):
- no access modifiers, everything is public
interface <INTERFACE_NAME> {
...
implements <INTERFACE_NAME>
...
Access Modifier
- private
- protected
- public
Generics
Functions
Function Parameters
function doSomething(value: any, options?: <TYPE>) // options parameter is optional
Decorators
A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use the form @<Expression>, where <Expression> must evaluate to a function that will be called at runtime with information about the decorated declaration.
Non-null assertion/Bang operator
When you add an exclamation mark after variable/property name, you're telling to TypeScript that you're certain that value is not null or undefined, which prevents the ts2564 compilation error.
Optional Chaining Operator
Import/Export
Starting with ECMAScript 2015, JavaScript has a concept of modules. TypeScript shares this concept. Each ts-file is javascript module.
import { ZipCodeValidator } from "./ZipCodeValidator"; // import a single export from a module
Installation
C:\Uwes\Programme\nodejs>npm install -g typescript