die tsconfig.json wird automatisch akzeptiert, und wenn wir einfach tsc ausführen, wird einfach die index.ts ausgeführt.
Das primitivste javascript, zu dem wir compilen können ist ES3, dann ist der code aber absolut gigantisch.
custom types:
type Name = string
let personName: Name
personName = “Max”
in diesem fall ist das ziemlich unnötig. Hier ein komplexeres beispiel:
type RelationshipStatus = “single” | “taken”
let MyStatus: RelationshipStatus
MyStatus = “taken”
custom types:
interface Person { firstname: string, lastname: string, age: number }
const Max: Person = { firstname: “Max”, age: 22 } // das gibt einen Fehler - lastname fehlt in unserem Objekt. // es gibt auch ein Fehler, wenn eine falsche property dabei ist.
// damit wir doch noch additional properties adden dürfen:
interface Person { firstname: string, lastname: string, age: number, [key: string]: any }
const Max: Person = { firstname: “Max”, lastname: “Doe”, age: 22, money: 2000 }
// damit einzelne props optional sind: interface Person { firstname: string, lastname: string, age: number, degree?: string }
// ein array in javascript ist leider immer noch nicht vollständig immutable:
const names: Array
names[0] = “carl”
console.log(names)
// aber wir können einen array tatsächlich immutable machen:
const names: ReadonlyArray
console.log(names)
// das problem mit den nicht-immutable const objects lässt sich auch lösen: das hier ist in javascript vollkommen OK: const obj = { name: “max”, age: 22 }
obj.name = “carl”
// so geht das in typescript, damit es nur lesbar ist:
interface Person { readonly name: string, readonly age: number }
const obj: Person = { name: “max”, age: 22 }
// so funktioniert ein tupel:
type PersonArray = [string, string, number]
let MaxArray: PersonArray MaxArray = [“Max”, “Doe”, 22]
// dabei ist es egal, ob wir das array direkt erstellen, oder // die werte erst im nachhinein zuweisen.
// function overloading: