1. ์ธํ„ฐํŽ˜์ด์Šค ๋ฐ ์ดˆ๊ธฐ๊ฐ’ ์„ค์ • 

type Score = 'A' | 'B' | 'C' | 'D' | 'F'  


// 1. Interface
interface User {
    name : string;     // ํ•„์ˆ˜
    age : number;      // ํ•„์ˆ˜
    gender? : string;  // ์˜ต์…˜
    readonly birthYear : number;  // ์ฝ๊ธฐ ์ „์šฉ (์ˆ˜์ •X)
    [grade : number] : Score;  // ํ‚ค:number, ๊ฐ’:Score ์ธ ํ”„๋กœํผํ‹ฐ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ
}


// 2. Interface ๊ธฐ์ค€์œผ๋กœ ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
let user : User = {  
    name : 'xx',
    age: 30,
    birthYear : 2000,
    1 : 'A',
    2 : 'B',
}

console.log(user.age) // 30

 

 

2. ์ธํ„ฐํŽ˜์ด์Šค๋กœ ํ•จ์ˆ˜ ์ •์˜

// 1. Interface
interface Add {
  (num1: number, num2: number): number;

}

// 2. Interface๋ฅผ ์ด์šฉํ•ด์„œ ํ•จ์ˆ˜ ์ƒ์„ฑ
const add : Add = (x,y) => {
  return x + y
}

console.log(add(3,5))  // 8

 

 

3. ์ธํ„ฐํŽ˜์ด์Šค๋กœ ํด๋ž˜์Šค ์ •์˜

// 1. Interface
interface Car {
  color: string;
  wheels: number;
  start(): void;
}

// 2. Interface ์ด์šฉํ•ด์„œ ํด๋ž˜์Šค ์ƒ์„ฑ
class Bmw implements Car {
  color;
  wheels = 4;
  constructor(c:string) {  // ์ƒ์„ฑ๋ ๋•Œ color๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ
    this.color = c;
  }
  start(){
    console.log('go..')
  }
}

const b = new Bmw('green')
console.log(b) // Bmw: { 'wheels': 4, 'color': 'green'}
b.start() //  "go.."

 

 

3-1. ์ธํ„ฐํŽ˜์ด์Šค ํ™•์žฅ

// 1. Interface
interface Car {
  color: string;
  wheels: number;
  start(): void;
}

// 1-2. Interface ํ™•์žฅ
interface Benz extends Car {
  door: number;
  stop(): void;
}

// 1-3. Interface ๊ธฐ์ค€์œผ๋กœ ์ดˆ๊ธฐ๊ฐ’ ์ž‘์„ฑ
const benz : Benz = {
  door : 5,
  color : 'black',
  wheels : 4,
  stop(){
    console.log('stop')
  },
  start(){
    console.log('go..')
  }
}

 

 

3-2. ์ธํ„ฐํŽ˜์ด์Šค ํ™•์žฅ

// 1. Car Interface
interface Car {
  color: string;
  wheels: number;
  start(): void;
}

// 2. Toy Interface
interface Toy {
  name: string;
}

// 3. Car์™€ Toy์˜ ์ธํ„ฐํŽ˜์ด์Šค ํ™•์žฅ
interface ToyCar extends Car, Toy {
  price : number;
}

 

1. ๋ฌธ์ž

let color = 'purple';  // 'purple'์„ ์ž๋™์ ์œผ๋กœ string์œผ๋กœ ์ธ์‹ (๋‹ค๋ฅธ ํƒ€์ž…๋„ ๋งˆ์ฐฌ๊ฐ€์ง€)
let color:string = 'purple'

 

 

2. ์ˆซ์ž

let age:number = 30

 

 

3. ๋ถˆ๋ฆฌ์–ธ

let isAdult:boolean = true;

 

 

4. ์ˆซ์ž/ ๋ฌธ์ž์—ด ๋ฐฐ์—ด

let arr:number[] = [1,2,3]  //์ˆซ์ž ๋ฐฐ์—ด

let arr:string[] = ['a','b','c']  //๋ฌธ์ž์—ด ๋ฐฐ์—ด1
let arr:Array<string> = ['a','b','c']  //๋ฌธ์ž์—ด ๋ฐฐ์—ด2

 

 

5. ํŠœํ”Œ

let b:[string, number] // ์ฒซ๋ฒˆ์งธ ์ธ์ž: ๋ฌธ์ž , ๋‘๋ฒˆ์งธ ์ธ์ž: ์ˆซ์ž

b = ['a',1]

 

 

6. void / never

void : ํ•จ์ˆ˜์—์„œ ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์„ ๋•Œ ์‚ฌ์šฉํ•จ

const sayHello = ():void =>{
	console.log('hello')
}

sayHello()

naver : ํ•ญ์ƒ ์—๋Ÿฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜ ๋๋‚˜์ง€์•Š๋Š” ํ•จ์ˆ˜ ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•จ

const showError = ():never => {
	throw new Error()
}

const infLoop = ():never => {
	while(true) {
    	// do something
    }
}

 

 

7. enum 

enum : ๋น„์Šทํ•œ ๊ฐ’์„ ๋ฌถ์–ด ์ฃผ๋Š”, ์—ด๊ฑฐํ˜• ๋ฐ์ดํ„ฐ

enum Os {
    Window = 3, // ๊ธฐ๋ณธ์ ์œผ๋กœ 0๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ํ• ๋‹น๋จ (3์œผ๋กœ ์ž‘์„ฑ์‹œ 3์œผ๋กœ ํ• ๋‹น, 3๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ํ• ๋‹น๋จ)
    Ios, 
    Android
}

console.log(Os[3])  // Window
console.log(Os['Window'])  // 3

let myOs:Os  // Os์˜ ๊ฐ’๋งŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค

myOs = Os.Window

 

 

8. null / undefined

let a:null = null
let b:undefined = undefined

Node.js๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ์š”๊ตฌ๊ฐ€ ๋Š˜์–ด๋‚˜๋Š” ๊ฒƒ๊ฐ™์•„ ๋ฐฐ์›Œ๋ณด๊ณ ์ž ํ•œ๋‹ค.

 


 

Javascript๋Š” ๋™์ ์–ธ์–ด๋ผ, ๋Ÿฐํƒ€์ž„์— ํƒ€์ž…์ด ๊ฒฐ์ •๋˜๊ณ  ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. 

์ฝ”๋“œ์ƒ์—๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์ง€๋งŒ, ์˜๋„ํ•˜์ง€ ์•Š์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

 

 

ํ•˜์ง€๋งŒ, typescript(๋‚˜ Java)๋Š” ์ •์ ์–ธ์–ด๋ผ ์ปดํŒŒ์ผ ํƒ€์ž„์— ํƒ€์ž…์ด ๊ฒฐ์ •๋˜๊ณ  ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

์•„๋ž˜์ฒ˜๋Ÿผ ์•„์ง ์‹คํ–‰์‹œํ‚ค์ง€๋„ ์•Š์•˜๋Š”๋ฐ, ์˜ค๋ฅ˜๋“ค์ด ๋‚˜ํƒ€๋‚œ๋‹ค.

 

 

typescript ์‚ฌ์šฉํ• ๋•Œ์—๋Š” type์„ ์„ค์ •ํ•ด์ค˜์•ผํ•œ๋‹ค.

์˜๋„ํ•œ ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. 

 

+ Recent posts