Swift is like TypeScript
BASICS
Hello World
Swift
print("Hello, world!")
TypeScript
console.log("Hello, world!");
Variables And Constants
Swift
var myVariable = 42
myVariable = 50
let myConstant = 42
TypeScript
var myVariable = 42;
myVariable = 50;
const myConstant = 42; // Constant

/* NOTE::
* let and var are very similar when used like this outside 
* a function block
*/

let name = 'TypeScript';  // globally scoped
var age = 5; // globally scoped

/* 
* Also, They are identical when used like this in 
* a function block.
*/

function ingWithinEstablishedParameters() {
    let name = 'TypeScript'; //function block scoped
    var age = 5; //function block scoped
}

/* 
* But the difference is when you used it inside a block. 
* In the example below; let is only visible in the for() loop 
* and var is visible to the whole function.
*/

function myFunction() {
    //x is *not* visible out here

    for( let x = 0; x < 5; x++ ) {
        //x is only visible in here and in the for() parentheses
        //and there is a separate tuce variable for each 
        // iteration of the loop
    }

    //x is *not* visible out here
}

function myFunction() {
    //x *is* visible out here

    for( var x = 0; x < 5; x++ ) {
        //x is visible to the whole function
    }

    //x *is* visible out here
}
Explicit Types
Swift
let explicitDouble: Double = 70
TypeScript
let explicitDouble: number = 70;
Type Coercion
Swift
let label = "The width is "
let width = 94
let widthLabel = label + String(width)
TypeScript
let label = "The width is ";
let width = 94;
let widthLabel = label + String(width);
String Interpolation
Swift
let apples = 3
let oranges = 5
let fruitSummary = "I have \(apples + oranges) " +
                   "pieces of fruit."
TypeScript
let apples = 3;
let oranges = 5;
let fruitSummary = "I have " + (apples + oranges) +
 " pieces of fruit.";
Range Operator
Swift
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {
    print("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
TypeScript
let names = ["Anna", "Alex", "Brian", "Jack"];
names.forEach((value: string, index: number) => {
    console.log("Person " + index + " is called " + value);
});
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
Inclusive Range Operator
Swift
for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
TypeScript
for (let index = 1; index <= 5;index ++){
    console.log(index + " times 5 is " + (index * 5));
}

// OR //

Array.from({ length: 5 }, (value, key) => {
        console.log((key+1) + " times 5 is " + ((key+1) * 5));
});
COLLECTIONS
Arrays
Swift
var shoppingList = ["catfish", "water",
    "tulips", "blue paint"]
shoppingList[1] = "bottle of water"
TypeScript
var shoppingList: string[] = ["catfish", "water",
    "tulips", "blue paint"];
shoppingList[1] = "bottle of water";
Maps
Swift
var occupations = [
    "Malcolm": "Captain",
    "Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
TypeScript
var occupations:{ [key: string]: string; } = {}
occupations["Malcolm"] = "Captain";
occupations["Kaylee"] = "Mechanic";
occupations["Jayne"] = "Public Relations";
Empty Collections
Swift
let emptyArray = [String]()
TypeScript
let emptyArray: string[] = []
FUNCTIONS
Functions
Swift
func greet(_ name: String,_ day: String) -> String {
    return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
TypeScript
function greet(name: string, day: string): string {
    return "Hello " + name + ", today is " + day + ".";
}
greet("Bob", "Tuesday");
Tuple Return
Swift
func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
TypeScript
---------
Variable Number Of Arguments
Swift
func sumOf(_ numbers: Int...) -> Int {
    var sum = 0
    for number in numbers {
        sum += number
    }
    return sum
}
sumOf(42, 597, 12)
TypeScript
function sumOf(...numbers: number[]): number {
    var sum = 0
    for (let number of numbers) {
        sum += number;
    }
    return sum
}
sumOf(42, 597, 12);
Function Type
Swift
func makeIncrementer() -> (Int -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
let increment = makeIncrementer()
increment(7)
TypeScript
let makeIncrementer: (value: number) => number =
    function (value: number): number {
        return value + 1; 
    };



let increment = makeIncrementer;
console.log(increment(7));
Map
Swift
let numbers = [20, 19, 7, 12]
numbers.map { 3 * $0 }
TypeScript
let numbers = [20, 19, 7, 12];
numbers.map(value => console.log(value * 3));
Sort
Swift
var mutableArray = [1, 5, 3, 12, 2]
mutableArray.sort()
TypeScript
var mutableArray = [1, 5, 3, 12, 2];
mutableArray.sort((n1,n2) => n1 - n2);
Named Arguments
Swift
func area(width: Int, height: Int) -> Int {
    return width * height
}
area(width: 2, height: 3)
TypeScript
------
CLASSES
Declaration
Swift
class Shape {
    var numberOfSides = 0
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}
TypeScript
class Shape {
    private numberOfSides = 0;

    simpleDescription(): string {
        return "A shape with " + this.numberOfSides +
        " sides.";
    }
}
Usage
Swift
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
TypeScript
var shape = new Shape();
shape.numberOfSides = 7;
var shapeDescription = shape.simpleDescription();
Subclass
Swift
class NamedShape {
    var numberOfSides: Int = 0
    let name: String

    init(name: String) {
        self.name = name
    }

    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}

class Square: NamedShape {
    var sideLength: Double

    init(sideLength: Double, name: String) {
        self.sideLength = sideLength
        super.init(name: name)
        self.numberOfSides = 4
    }

    func area() -> Double {
        return sideLength * sideLength
    }

    override func simpleDescription() -> String {
        return "A square with sides of length " +
	       sideLength + "."
    }
}

let test = Square(sideLength: 5.2, name: "square")
test.area()
test.simpleDescription()
TypeScript
class NamedShape {
    numberOfSides: number = 0;
    name: string;

    constructor(name: string) {
        this.name = name;
    }

    simpleDescription(): string {
        return "A shape with " + this.numberOfSides +
         " sides.";
    }
}

class Square extends NamedShape {
    sideLength: number;

    constructor(sideLength: number, name: string) {
        super(name);
        this.sideLength = sideLength;
        this.numberOfSides = 4;
    }

    area() : number {
        return this.sideLength * this.sideLength
    }

    simpleDescription(): string {
        return "A square with sides of length " +
        this.sideLength + "."
    }
}

let test = new Square(5.2, "square");
test.area();
test.simpleDescription();
Checking Type
Swift
var songCount = 0

for item in library {
    if item is Movie {
        movieCount += 1
    } else if item is Song {
        songCount += 1
    }
}
TypeScript
var movieCount = 0
var songCount = 0

for (let item of library) {
    if (item instanceof Movie){
        ++movieCount;
    } else if(item instanceof Song){
        ++songCount;
    }
}
Pattern Matching
Swift
let nb = 42
switch nb {
    case 0...7, 8, 9: print("single digit")
    case 10: print("double digits")
    case 11...99: print("double digits")
    case 100...999: print("triple digits")
    default: print("four or more digits")
}
TypeScript
let nb = 42
switch (true) {
    case nb >= 0 && nb <= 9:
        console.log("single digit");
    case nb === 10:
        console.log("double digits");
    case nb >= 11 && nb <= 99:
        console.log("double digits");
    case nb >= 100 && nb <= 999:
        console.log("triple digits");
    default:
        console.log("four or more digits");
}
Downcasting
Swift
for current in someObjects {
    if let movie = current as? Movie {
        print("Movie: '\(movie.name)', " +
            "dir. \(movie.director)")
    }
}
TypeScript
for (let current of someObjects) {
    if(current instanceof Movie) {
        console.log("Movie: " + current.name + ", " +
            "dir. " + current.director);
    }
}
Protocol
Swift
protocol Nameable {
    func name() -> String
}

func f<T: Nameable>(x: T) {
    print("Name is " + x.name())
}
TypeScript
interface Nameable {
    name(): string;
}

printName(x: Nameable) {
        console.log("Name is " + x.name());
}
Extensions
Swift
extension Double {
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")
// prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// prints "Three feet is 0.914399970739201 meters"
TypeScript
interface Number {
    mm(): number;
    ft(): number;
       
}

Number.prototype.mm = function (): number {
    return this / 1000;
}
Number.prototype.ft = function (): number {
    return this / 3.28084;
}

let oneInch = 25.4.mm();
console.log("One inch is " + oneInch + " meters");
// prints "One inch is 0.0254 meters"
let threeFeet = 3.0.ft();
console.log("Three feet is " + threeFeet + " meters");
// prints "Three feet is 0.914399970739201 meters"