let's get IT with DAVINA ๐Ÿ’ป

TypeScript (Function) ๋ณธ๋ฌธ

DEV_IN/TypeScript

TypeScript (Function)

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 9. 19:24

Call Signatures

  • ๋งˆ์šฐ์Šค๋ฅผ ๊ฐ–๋‹ค ์˜ฌ๋ ค๋ณด๋ฉด ์œ„์— ๋œจ๋Š” ์ € ๋ฐ•์Šค
  • ํ•จ์ˆ˜์˜ ์ธ์ž type ์ •์˜ + return type ์ •์˜
  • Type์„ ๋จผ์ € ์ •์˜ํ•œ ๋’ค -> ํ•จ์ˆ˜/์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์ž
    • instead of ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด์„œ ๋™์‹œ์— type์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค better 

//type ๋ฏธ๋ฆฌ ์ •์˜
type Add = (a:number, b:number) => number

const add:Add = (a,b) => a+b

//instead of 
const add = (a:number, b:number) => a+b

Overloading

  • ํ•จ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ call signatures๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๋•Œ ๋ฐœ์ƒ
  • ๋‚ด๊ฐ€ ์ง์ ‘ ์“ธ ์ผ์€ ๊ฑฐ์˜ ์—†์ง€๋งŒ, ์™ธ๋ถ€ ํŒจํ‚ค์ง€๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋งŽ์ด ๋ณผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•Œ์•„๋‘์ž!
type Config = {
	path: string, 
	state: object
}

type Push = {
	(path:string) : void //void=์•„๋ฌด๊ฒƒ๋„ returnํ•˜์ง€ ์•Š์Œ
	(config: Config) : void
}

const push:Push = (config) => {
	if(typeof config==="string") {console.log(config)}
	else {
		console.log(config.path,config.state)
	}
}
type Add = {
	(a:number, b:number) : number
	(a:number, b:number, c:number) : number
}

//parameter ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅผ๋•Œ? optionalํ•œ ์ธ์ž๋Š” ์ •ํ™•ํžˆ ๋‹ค์‹œ ์ •์˜ํ•ด์ฃผ๊ธฐ
const add:Add = (a,b,c?:number) => { 
	if(c) return a+b+c
	return a+b
}

Polymorphism

  • many + structure = ๋‹คํ˜•์„ฑ
  • type์ด ๋ญ๊ฐ€ ๋“ค์–ด์˜ฌ์ง€ ๋ชจ๋ฅด๋Š” ์ƒํƒœ๋กœ call signature์„ ์ž‘์„ฑํ•ด์•ผ ํ•  ๋•Œ
    • generic์„ ์‚ฌ์šฉํ•˜์ž!
  • generic ์ •์˜ ๋ฐฉ๋ฒ•
type [type์ด๋ฆ„๋Œ€๋ฌธ์ž] = {
	<[Generic์ด๋ฆ„]> (arr: [Generic์ด๋ฆ„][]) : void//void๋Œ€์‹  ์•”๊ฑฐ๋‚˜..๋ญ˜ returnํ•˜๋Š”์ง€์— ๋”ฐ๋ผ
}

function superPrint<T>(a: T[]){
	return a[0];
}
  • ๋‹ค์–‘ํ•œ generic usage
type SuperPrint={
	<T>(arr: T[]) : T
}

const superPrint:SuperPrint = (arr) => arr[0]

//๋ฐ‘์—์ฒ˜๋Ÿผ ์–ด๋–ค ํƒ€์ž…์˜ ์š”์†Œ๊ฐ€ ๋ฐฐ์—ด์˜ ์š”์†Œ๋กœ ๋“ค์–ด์™€๋„ ์ƒ๊ด€์—†๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” generic
//์ผ์ผ์ด ์œ„์— type์„ ๊ฒฝ์šฐ์˜ ์ˆ˜๋งˆ๋‹ค ๋‹ค ์ •์˜ํ•˜๊ธฐ๊ฐ€ ํž˜๋“ค๋‹ค!!
superPrint([1,2,3]) //number
superPrint(["1","2","3"]) //string
superPrint([true,false,true]) //boolean
superPrint([true,"1",1]) //number|boolean|string
type A = Array<number> //genericํ•œ ๋ฐฐ์—ด ์ƒ์„ฑํ•ด์คŒ

let a:A=[1,2,3,4]
function printAllNumbers(arr: Array<number>){}
type Player<E> = {
	name: string
	extraInfo: E
}

//generic ์„ค์ •ํ•ด์ฃผ๊ธฐ (์–ด๋–ค type์„ ์“ธ์ง€)
type ViniExtra = {
	favFood: string
}

type ViniPlayer = Player<ViniExtra>

const vini: ViniPlayer = {
	name: "vini",
	extraInfo: {
		favFood: "egg"
	}
}

Generics Recap

any๋ฅผ ์•ˆ์“ฐ๊ณ  generic์„ ์“ฐ๋Š” ์ด์œ ๋Š”?

- any๋Š” ์ •ํ™•ํžˆ ๋ฌด์Šจ type์ธ์ง€ ์ง€์ •ํ•ด์ฃผ์ง€ ์•Š๊ณ , 
- generic์€ ์–ด๋–ค type์ด ๋“ค์–ด์˜ค๋ƒ์— ๋”ฐ๋ผ ๊ทธ type์„ ์ถ”๋ก ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์—!
→ ํ•œ๋งˆ๋””๋กœ, generic์€ ๋‚ด๊ฐ€ ์›ํ•˜๋Š”๋Œ€๋กœ signature์„ ์ƒ์„ฑ/์‚ฌ์šฉ ๊ฐ€๋Šฅ!
  • generic์„ ์—ฌ๋Ÿฌ๊ฐœ ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด?
type SuperPrint={
	<T, M>(a: T[], b:M) : T
}

superPrint([1,"hi", true], "x")
superPrint([1,"hi", true], false)
//generic์ด ์ƒ์„ฑ๋˜๋ฉด ๊ทธ ์ˆœ์„œ์— ๋งž๊ฒŒ ์•Œ์•„์„œ TS๊ฐ€ generic์„ ์ฝ์–ด์ค˜

'DEV_IN > TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

โ›”๏ธ ์ž์ฃผ ๋ณผ ์ˆ˜ ์žˆ๋Š” Errors  (0) 2023.02.17
TypeScript(apply)  (2) 2023.02.17
TypeScript(Interface)  (4) 2023.02.16
TypeScript(Classes)  (4) 2023.02.12
TypeScript๋ž€? & Types  (0) 2023.02.09
Comments