ts
#typescript

number, string, boolean types

function basicType1(num1: number, str1: string, bool1: boolean) {
  const data = {
    num1,
    str1,
    bool1,
  }
  return data
}

const result1 = basicType1(10, 'hi', true)
console.log('type number,string, boolean = ', result1)

object types

const testObjectType: { name: string; age: number } = {
  name: 'khansa',
  age: 8,
}

testObjectType.name = 'Qanita'
testObjectType.age = 2
console.log('type object = ', testObjectType)

Symbol types

Simbol adalah pengidentifikasi unik yang dapat digunakan sebagai kunci properti dalam objek untuk mencegah konflik penamaan.

type Obj = {
  [sym: symbol]: number
}

const a = Symbol('a')
const b = Symbol('b')
let obj: Obj = {}
obj[a] = 123
obj[b] = 456

console.log(obj[a]) // 123
console.log(obj[b]) // 456

array types

const arrayType: string[] = ['a', 'b']
const arrayType2: Array<string> = ['a', 'b']
const arrayType3: Array<string | number> = ['a', 'b']
console.log('type array = ', arrayType)

tuples types

// @errors: 2345
const arrayTuplesType: [number, string] = [1, 'b']
arrayTuplesType.push(1)
arrayTuplesType.push(true) // error
console.log('type array = ', arrayTuplesType)

enum types

// numeric enum
enum testEnumNumber {
  a, // 0
  b = 3,
  c = 5,
}
enum testEnumString {
  naruto = 'Rasengan',
  sasuke = 'Chidori',
}
enum testEnumGabungan {
  a, // 0
  sasuke = 'Chidori',
}

console.log('type enum = ', testEnumGabungan.a) // 0

any Types

union Types

const testUnion = (a: number, b: number | string) => {
  console.log(a, b)
}
testUnion(12, 'aa')

literal Types

type testLiteral1 = 'Samsung' | 'Xiaomi' | 'Sony' // type alias

function testLiteral(phone: testLiteral1, price: number): string {
  return `My phone = ${phone} and price = ${price}`
}
const resultTestLiteral = testLiteral('Samsung', 2000000)
console.log(resultTestLiteral)

type alias

type testalias1 = 'Samsung' | 'Xiaomi' | 'Sony'
type testalias2 = { name: string; desc: string }

function testAliasProfil(profil: testalias2): void {
  console.log(profil.name + ' - ' + profil.desc)
}
testAliasProfil({ name: 'test alias', desc: 'wow ini toh type alias' })

mengembalikan Function dengan types atau void

function testReturnType(a: number): string {
  return a.toString()
}
const resReturnType = testReturnType(100)
console.log('test return types/void = ', resReturnType)

membuat Function sebagai types dan memberikan callback

function testFunctionAsTypes1(n1: number, n2: number) {
  return n1 + n2
}
function testFunctionAsTypes2(n1: number): void {
  console.log('Result = ', n1)
}
let testFunctionAsTypes3: (n1: number, n2: number) => number
testFunctionAsTypes3 = testFunctionAsTypes1
testFunctionAsTypes3 = testFunctionAsTypes3
console.log(testFunctionAsTypes3(2, 4)) // Result = 6

unknown type

// contoh:
let testUnknownType1: unknown
let testUnknownType2: string

testUnknownType1 = 109
testUnknownType1 = '109'

dibawah ini error karena type unknown tidak dapat ditetapkan untuk tipe string.

// @errors: 2322
let testUnknownType1: unknown
let testUnknownType2: string

testUnknownType2 = testUnknownType1 // error

// untuk memperbaikinya kita bisa memeriksanya dengan typeof
if (typeof testUnknownType1 === 'string') {
  testUnknownType2 = testUnknownType1
}

note: tapi lebih baiknya jika kita tidak tahu apa yang ingin kita masukan ke variabel testUnknownType1 apakah string/number/lainnya kita bisa gunakan union type terlebih dahulu dibanding unknown.

never type

  1. Fungsi yang melempar kesalahan, contohnya dibawah ini.
function generateError(msg: string, code: number): never {
  throw { msg, code }
}

try {
  const resError = generateError('Error bro', 500)
  console.log('error = ', resError)
} catch (error) {
  console.log('errors = ', error)
}

assertion Type

Type Assertion berguna untuk kita membuat sebuah variabel namun tidak langsung kita masukan nilainya atau dalam artian kita hanya membuat kerangkanya saja. type Assesrtion menggunakan syntax as

interface Foo {
  bar: number
  bas: string
}
// kita bisa membuat kerangkanya saja
const foo = {} as Foo

foo.bar = 1
foo.bas = 'hi'
console.log(foo)