Шпаргалка typescript

Dev IT JavaScript 564

let n: number = 1
console.log("number:", n)

let s: string
s = "lorem ipsum"
console.log("string:", s)

let b: boolean
b = true
console.log("bool:", b)

let any: any
any = "0123456789"
console.log("any:", any)

const numberArray: number[] = [1, 2, 3, 4, 5]
console.log("number array:", numberArray)

const stringArray: string[] = ["a", "b", "c", "d", "e"]
console.log("string array:", stringArray)

const anyArray: any[] = [1, 2, 3, true, "string"]
console.log("any array:", anyArray)

// окремий масив з визначеною кількістю елементів
const tuple: [number, null, string] = [1, null, "lorem ipsum"]
console.log(tuple)

// масив з масивами
let tupleArray: [string, number][]
tupleArray = [
	["Vasyl", 10],
	["Myhailo", 20],
	["Dmytro", 30],
]
console.log(tupleArray)

// допустимі типи у змінної
let variable: string | number | null
variable = "Lorem ipsum"
console.log(variable)

// іменовані константи (default -- numeric)
enum Dir {
	TOP = "top",
	LEFT = "left",
	BOTTOM = "bottom",
	RIGHT = "right",
}

console.log(Dir)
console.log(Dir.TOP)

// object
const obj1: {
	id: number
	name: string
} = {
	id: 1,
	name: "Vasyl",
}
console.log(obj1)

// type
type Obj = {
	id: number
	name: string
}

const obj2: Obj = {
	id: 2,
	name: "Petro",
}
console.log(obj2)

// type assertion
let cid: any = 1
let uniqueCid = <number>cid
uniqueCid = 2
console.log(cid, uniqueCid)

let customerCid = cid as number
customerCid = 3
console.log(customerCid)

// function
function sum(a: number, b: number): number {
	return a + b
}
console.log(sum(2, 2))

function message(message: string | number | null | undefined): void {
	console.log(message)
}

// interface
interface Interface {
	readonly id: number // read only field!
	name: string
	age?: number // not require, optionally
}

const obj3: Interface = {
	id: 3,
	name: "Mykola",
}
console.log(obj3)

// interface for function
interface MathFunc {
	(a: number, b: number): number
}

// instance interface for function
const add: MathFunc = (x: number, y: number): number => x + y
const sub: MathFunc = (x: number, y: number): number => x - y

console.log(add(3, 4))
console.log(sub(6, 5))

// classes
class UserData {
	protected id: number
	private name: string
	// age?: number // не потрібно

	constructor(id: number, name: string) {
		this.id = id
		this.name = name
	}

	getName(): string {
		return this.name
	}
}

// instance class
const duncan = new UserData(1, "duncan")
console.log(duncan)
console.log(duncan.getName())

// interface and class
interface Param {
	id: number
	name: string
	age?: number
	getName(): string
}

class Customer implements Param {
	id: number
	name: string

	constructor(id: number, name: string) {
		this.id = id
		this.name = name
	}

	getName(): string {
		return this.name
	}
}

const newUser = new Customer(999, "Anonimus")
console.log(newUser)

// subclasses
class Person extends Customer {
	profession: string

	constructor(id: number, name: string, profession: string) {
		super(id, name)
		this.profession = profession
		this.id = id
		this.name = name
	}
}

const newPerson = new Person(333, "somthing user", "outcast")
console.log(newPerson)

// generics
function getArray<Type>(items: Type[]): Type[] {
	// return new Array().concat(items)
	return [...items]
}

const numArray = getArray<number>([1, 2, 3, 445, 5])
const strArray = getArray<string>(["a", "b", "c", "d", "e", "f", "g"])

console.log(numArray, strArray)

strArray.push("h")

console.log(strArray)