TypeScript: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
 
(9 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
= Links =
= Links =
* [[Angular]]
* [[Angular]]
* [[RestSv]]
* [[MySqlRestServer]]


= Konstanten, Variablen =
= Konstanten, Variablen =
* 'var'-Variablen sind global, 'let'-Variablen sind lokal (blockgebunden).
* 'var'-Variablen sind global, 'let'-Variablen sind lokal (blockgebunden).
<source lang="ts">
<syntaxhighlight lang="ts">
const E_DISK: string = 'disk error';
const E_DISK: string = 'disk error';
var globalLastMessage: string = 'Error';
var globalLastMessage: string = 'Error';
Zeile 15: Zeile 17:
const [width, height] = [1, 2];
const [width, height] = [1, 2];
const [width, ...rest] = [1, 2, 3, 4]; // rest hat den Wert [2, 3, 4]
const [width, ...rest] = [1, 2, 3, 4]; // rest hat den Wert [2, 3, 4]
</source>
</syntaxhighlight>
 
= Statements =
<syntaxhighlight lang="ts">
for (let i = 0; i < 3; i++){
  doit(i);
}
let arr = [10, 20, 30, 40];
// for in loops over enumerable property names of an object.
// Loops over keys:
for (var val in arr) {
  doit(val);
}
// for of (new in ES6) does use an object-specific iterator and loops over the values generated by that.
// Loops over values:
for (var val in iterator) {
  doit(val);
}
 
</syntaxhighlight>


= Typen =
= Typen =
== String ==
== String ==
<source lang="ts">
* " und ' gleichwertig
* `text ${expr}` wird interpretiert
 
<syntaxhighlight lang="ts">
  let x ='Hi';
  let x ='Hi';
  x = `2 lines:
  x = `2 lines:
Say: ${x}
Say: ${x}
`;
`;
</source>
</syntaxhighlight>
 
== Number ==
== Number ==
== Array ==
<pre>
let a: string[];
a.push('Hi');
a[0] = 'Hello'; a[1] = 'world';
let b = [12.4, 100, -33.4];
let len = b.length;
let series = [1, 2, 3];
var series2 = series.concat(47, 49);
var index = series2.indexOf(47);
// forEach(), map(), reduce(), and filter()
let doubleIt = series.map(e => e* 2);
let scores : (string | number)[];
scores = ['Programming', 5, 'Software Design', 4];
</pre>
* Assoziatives Array:
<pre>
var information = new Array();
information["key1"] = 25;
const array1 = { “one”: 1, “two”: 2, “three”: 3 };
const iterator = array1.keys();
for (const key of iterator) {
  console.log(key);
}
</pre>
== Any ==
== Any ==


== Enums ==
== Enums ==
<pre>typedef Status = 'active' | 'inactive';
<pre>typedef Status = 'active' | 'inactive';
</pre>
== Funktionen/Methoden ==
<pre>
# default parameters:
getList(offset: number=0, count: number=20){
  ...
}
</pre>
</pre>


= Klassen =
= Klassen =
* Nur ein Konstruktor
* Nur ein Konstruktor
<source lang="ts">
<syntaxhighlight lang="ts">
class User {
class User {
   publicItems: string;
   publicItems: string;
Zeile 56: Zeile 118:
   }
   }
}
}
</source>
</syntaxhighlight>
== Interfaces ==
== Interfaces ==
<source lang="ts">
<syntaxhighlight lang="ts">
interface Contact {
interface Contact {
   firstname: string;
   firstname: string;
Zeile 71: Zeile 133:
   lastname = 'Smith';
   lastname = 'Smith';
}
}
</source>
</syntaxhighlight>


= Operatoren =
= Operatoren =
== instanceof ==
<syntaxhighlight lang="ts">
if (element instanceof HTMLElement){
  doIt();
}
</syntaxhighlight>
== spread-Operator ==
== spread-Operator ==
<source lang="ts">
<syntaxhighlight lang="ts">
let list1: number[] = [1, 2, 3];
let list1: number[] = [1, 2, 3];
let list2: number[] = [...list, 4, 5, ..list];
let list2: number[] = [...list, 4, 5, ..list];
myFunc(1, 2, 3);
myFunc(1, 2, 3);
myFunc(...list1);
myFunc(...list1);
</source>
</syntaxhighlight>

Aktuelle Version vom 11. Juni 2022, 14:04 Uhr

Links[Bearbeiten]

Konstanten, Variablen[Bearbeiten]

  • 'var'-Variablen sind global, 'let'-Variablen sind lokal (blockgebunden).
const E_DISK: string = 'disk error';
var globalLastMessage: string = 'Error';
for (let i=0; i < 4; i++){}
const myArray = [ 0, 'string', { firstname: 'Joe' } ];
const onlyNumbers: number[] = [ 1, 2, 3];
let arg: any = 5; ... arg = 'string';
let width, height: number;
let rest: number[];
const [width, height] = [1, 2];
const [width, ...rest] = [1, 2, 3, 4]; // rest hat den Wert [2, 3, 4]

Statements[Bearbeiten]

for (let i = 0; i < 3; i++){
  doit(i);
}
let arr = [10, 20, 30, 40];
// for in loops over enumerable property names of an object.
// Loops over keys:
for (var val in arr) {
  doit(val);
}
// for of (new in ES6) does use an object-specific iterator and loops over the values generated by that.
// Loops over values:
for (var val in iterator) {
  doit(val);
}

Typen[Bearbeiten]

String[Bearbeiten]

  • " und ' gleichwertig
  • `text ${expr}` wird interpretiert
 let x ='Hi';
 x = `2 lines:
Say: ${x}
`;

Number[Bearbeiten]

Array[Bearbeiten]

let a: string[];
a.push('Hi');
a[0] = 'Hello'; a[1] = 'world';
let b = [12.4, 100, -33.4];
let len = b.length;
let series = [1, 2, 3];
var series2 = series.concat(47, 49);
var index = series2.indexOf(47);

// forEach(), map(), reduce(), and filter() 
let doubleIt = series.map(e => e* 2);
let scores : (string | number)[];
scores = ['Programming', 5, 'Software Design', 4]; 
  • Assoziatives Array:
var information = new Array();
information["key1"] = 25;

const array1 = { “one”: 1, “two”: 2, “three”: 3 };
const iterator = array1.keys();

for (const key of iterator) {
  console.log(key);
}

Any[Bearbeiten]

Enums[Bearbeiten]

typedef Status = 'active' | 'inactive';

Funktionen/Methoden[Bearbeiten]

# default parameters:
getList(offset: number=0, count: number=20){
  ...
}

Klassen[Bearbeiten]

  • Nur ein Konstruktor
class User {
  publicItems: string;
  private privateMember: string = 'secret';
  private privateNumber = 25; // impliziter Typ.
  constructor(items: string){
    this.privateItems = items;
  }
  append(item: string): string { this.publicItems += ';' + item; return this.publicItems; }
  // getter + setter:
  get currentNumber(): number { return this.privateNumber; }
  set number(value: number): void { this.privateNumber = value; }
  add(inc: number) => return privateNumber + number;
  printAll(...args): void {
    print(args[0] + ': ' + args);
  }
}
class SuperUser extends User {
  constructor(items: string){
    super(items);
  }
}

Interfaces[Bearbeiten]

interface Contact {
  firstname: string;
  lastname: string;
}
const contact: Contact = {
  firstname: 'Joe',
  lastname: 'Doo'
}
class Address implements Contact {
  firstname = 'Eve';
  lastname = 'Smith';
}

Operatoren[Bearbeiten]

instanceof[Bearbeiten]

if (element instanceof HTMLElement){
  doIt();
}

spread-Operator[Bearbeiten]

let list1: number[] = [1, 2, 3];
let list2: number[] = [...list, 4, 5, ..list];
myFunc(1, 2, 3);
myFunc(...list1);