Not everything from Microsoft has to be bad.
let someVariable:string|number
someVariable = "Some text"
typeof(someVariable) // string
someVariable = 420
typeof(someVariable) // number
enum Directions {
North,
South,
West,
East
}
Directions.North // 0
Directions.West // 2
enum Directions {
North=1,
South,
West=10,
East
}
Directions.South // 2
Directions.East // 11
interface Person {
name: String,
age: number
}
const max: Person = {
name: "Max",
age: 22
}
type RelationshipStatus = "single" | "taken"
interface Person {
name: String,
age: number,
status: RelationshipStatus
}
// or to offer types:
type BankAccount = BigInteger | void
function printUsername(username) {
console.log(username)
}
printUsername("Max") // "Max"
function printUsername(username: string):void {
console.log(username)
}
printUsername("Max") // "Max"
const printUsername = (username) => {
console.log(username)
}
printUsername("Max")
const printUsername = (username: string): void => {
console.log(username)
}
printUsername("Max")
const names: Array<string> = ["Max", "Carl"]
// Or, if you want an immutable Array:
const names: ReadonlyArray<string> = ["Max", "Carl"]
const person: {
name: String,
age: Number
} = { name: "Max", age: 22 }
const numbers: Set<number> = new Set([1, 2, 3])
numberSet.add(4)
// or, to make it immutable:
const numbers: ReadonlySet<number> =
new Set([1, 2, 3])
// won't work:
numbers.add(4)