Skip to content
타입스크립트 기본기술 정리

Reviewing TypeScript basic types: tuple, enum, never, symbol, unknown

1 min readTech notesMoved from my Tistory blogRead the Korean original

Today I took some time to review TypeScript's basic types again!

tsx
// TypeScript review

interface PersonType{
    name:string;
    age:number;
    isStudent:boolean;
    studentNum?:number;
}

const person = {
    name:'엄성준',
    age:25,
    isStudent:false,
}

const getPersonAge = (peson:PersonType)=>{
    console.log(person.age)
}

const array1:string[] = ['a','b','c'] // array made only of strings
const array2:number[] = [1,2,3] // array made only of numbers
const array3:boolean[] = [true, false, true] // array made only of boolean values
const array4:(string | number)[] = ['a',1] // array made only of string or number

let user:[string, number, boolean] = ['엄성준',3,true] // tuple, an array with a fixed type and length
// console.log(user)

enum BloodType{ // an enum type is a set of named constants listed together
    A, // 0
    B =20, // 1
    O, // 2
    AB // 3
}

const myBloodType = BloodType.O

const sum = (a:number, b:number):number=>{
    return a+b
}

// void: no type can exist, used when a function has no return value
const noReturnSum = (a:number, b:number):void=>{
    console.log(a+b)
}

let neverValue:never; // a type that can't hold any value, the empty set

let sym = Symbol()
let sym2 = Symbol('test')
let sym3 = Symbol('test')

// console.log(sym2 === sym3)

let obj={
    name:'엄성준',
    [sym]:'test'
}

console.log(obj[sym])

function f1(a:any){
    a.toUpperCase()
}

function f2(a:unknown){
    if(typeof a === 'string'){
        return a.toUpperCase()
    }else{
        return '문자가 아닙니다!'
    }
}
  • #symbol
  • #type
  • #TypeScript basic types

More in Tech notes