Swift • TypeScript Cheatsheet
Type Annotations
// Basic
Any
Bool
String
// Numbers
Int
Double
// Arrays
[T]
Array<T>
// Dictionary
[T: U]
Dictionary<T, U>
// Tuples
(T, U, V)
// Functions
(arg: T) -> U
// Void
Void
// Optional
T?
Optional<T>
// Generics
Foo<T, U, V>
// Basic
unknown
boolean
string
// Numbers
number
// Arrays
T[]
Array<T>
// Object
{ [key: T]: U }
// Tuples
[T, U, V]
// Functions
(arg: T) => U
// Void
undefined
void // (functions with no return)
// Optional
T | undefined
// Generics
Foo<T, U, V>
// Other
any
null
Type Aliases
typealias StringArray = [String]
type StringArray = string[]
Constants
let myString = "hello"
const myString = "hello"
Variables
var myString = "hello"
let myString = "hello"
Functions
func myFunction(
arg1: String
) -> String {}
// Argument label
func myFunction(
label arg1: String
) -> String {}
// Default value
func myFunction(
arg1: String = "hello"
) -> String {}
// Argument with optional type
func myFunction(
arg1: String?
) -> String {}
// Optional type and default value
func myFunction(
arg1: String? = nil
) -> String {}
function myFunction(
arg1: string
): string {}
// Default value
function myFunction(
arg1: string = "hello"
): string {}
// Argument with optional type
function myFunction(
arg1: string | undefined
): string {}
// Optional argument
function myFunction(
arg1?: string
): string {}
Anonymous functions
let f: (A, B) -> C = {
a, b in
// ...
}
// Unused parameters
let f: (A, B) -> C = {
a, _ in
// ...
}
const f: (a: A, b: B) => C =
(arg1, arg2) => {
// ...
}
// Unused parameters
const f: (a: A, b: B) => C =
(arg1) => {
// ...
}
Classes
class Rect {
var x: Double
var y: Double
var width: Double
var height: Double
// Optional value
var name: String?
// Variable with initializer
var colorString: String = "black"
// Constant value
let dimensions = 2
init(
x: Double,
y: Double,
width: Double,
height: Double,
) {
self.x = x
self.y = y
self.width = width
self.height = height
}
// Method
func area() -> Double {
return self.width * self.height
}
// Getter
var midY: Double {
return self.y + self.height / 2
}
// Static method
static func zero() -> Rect {
return Rect(0, 0, 0, 0)
}
}
let rect = Rect(10, 20, 30, 40)
let zeroRect = Rect.zero()
class Rect {
x: number
y: number
width: number
height: number
// Optional value
name?: string
// Variable with initializer
colorString: string = "black"
// Constant value
readonly dimensions = 2
constructor(
x: number,
y: number,
width: number,
height: number,
) {
this.x = x
this.y = y
this.width = width
this.height = height
}
// Method
area(): number {
return this.width * this.height
}
// Getter
get midY(): number {
return this.y + this.height / 2
}
// Static method
static zero() {
return new Rect(0, 0, 0, 0)
}
}
const rect = new Rect(10, 20, 30, 40)
const zeroRect = Rect.zero()
Structs and Objects
struct MyStruct {
var a: String
var b: String
}
let x = MyStruct(
a: "hello",
b: "world"
)
type MyObject = {
a: string;
b: string;
};
let x: MyObject = {
a: "hello",
b: "world"
}
Protocols and Interfaces
protocol MyProtocol {
var a: String { get }
var b: String { get set }
func c(arg1: String) -> String
}
interface MyInterface {
readonly a: string;
b: string;
c(arg1: string): string;
}