/*
tsc --init # ініціалізація, створення конфігу
tsc index # компіляція у javascript
tsc --watch index # компіляція "на льоту"
*/
// types:
// important: тип вказувати не мусово, тайпскрипт автоматично визначить початковий
let name:string = 'my name' //only string
let age:number | null = 20 //number or null
age = null // true
let anything:any = 'anything types' // important: not recommended!
// нумерований масив
enum users {
Ivan, // auto increment 0
Petro, // auto increment 1
Vasyl // auto increment 2
}
enum person {
Max = 23, // value 23
Dennis = 30 // value 30
}
person.Max // 23
// об'єкт з типами даних
const user:{
name: string,
age: number,
country: 'Belarus' | 'Lytvania' | 'Estonia', // допустимі значення поля об'єкту
liked?: string // не обов'язковий параметр
} = {
name: 'Vasia',
age: 26,
country: 'Belarus',
liked: 'food'
}
// масив з певним типом
const arr1 = ['Vasia', 'Petro', 'Maria'] // auto check -- string values
const arr2:string[] = ['Vasia', 'Petro', 'Maria'] // set string values
// or
const arr3:Array<string> = ['Vasia', 'Petro', 'Maria'] // set string values
arr3.push(1) // error! not string
// many types
const arr4:(number | string)[] = [1,2,3, 'Vasia'] // number or string values
// :void -- a function that returns nothing
const notReturn(num1:number, num2:number):void => {
num1 + num2
}
// :null -- a function that returns nothing
const notReturn1(num1:number, num2:number):null => null
// custom types
type TypeCity = {
born: number
population: number
title: string
}
// create
const newYork:TypeCity = {
born: 1650
population: 10000000
title: 'New York'
}
// instance
newYork.born // 1650
// create array
const cities:TypeCity[] = [
{newYork},
{
born: 320
population: 2000000
title: 'London'
}]
// interface
interface interfaceName {
name: string
price: number
image: {
path: string
size: string
name: string
}
}
type TypeImage = {
path: string
size?: string // not required
name: string
}
// replace image, use type TypeImage
interface InterfaceName1 {
name: string
price: number
image: TypeImage
}
// inheritance
interface InheritanceInterface extends interfaceName1 {
// <adding parent fields>
// new field:
id: number
}
// instance
const instanceInheritanceInterface:InheritanceInterface = {
name: 'somthing name 1',
price: 1000,
image: {
path: 'https://google.com',
name: 'image somthing name'
},
id: 001
}
//approval (ствердження)
let instanceInheritanceInterface1:InheritanceInterface = {} as InheritanceInterface
// instace
instanceInheritanceInterface1.name = 'name 2'
instanceInheritanceInterface1.price = 2000
instanceInheritanceInterface1.image.path = 'https://meta.ua'
instanceInheritanceInterface1.image.name = 'image somthing name 2'
instanceInheritanceInterface1.id = 002
let instanceInheritanceInterface2:InheritanceInterface = {} as InheritanceInterface
instanceInheritanceInterface2 = {
name: 'somthing name 3',
price: 3000,
image: {
path: 'https://google.com',
name: 'image somthing name 3'
},
id: 003
}
// дженерики
const getArray = <T>(items: T[]): T[] =&amp;amp;gt; [...items]
const numbersArray = getArray&amp;lt;number&amp;gt;([1, 2, 3, 4])
const stringsArray = getArray&amp;lt;string&amp;gt;(["Vasia", "Petia"])
console.log(numbersArray, stringsArray)