๐ง ์ธํฐํ์ด์ค
๐ซ ์ธํฐํ์ด์ค๋
์ปดํจํฐ์ธ์์์์ ์ธํฐํ์ด์ค๋ ์ํธ๊ฐ์ ๊ท์น์ด๋ฉฐ, ํ์
์คํฌ๋ฆฝํธ์์๋ ์ฌ๋ฌ๊ฐ์ง ํ์
์ผ๋ก ์ด๋ฃจ์ด์ง ์๋ก์ด ํ์
.
์ฌ๋ฌ๊ฐ์ง์ ํ๋กํผํฐ์ ๋ฉ์๋๋ค๋ก ์ด๋ฃจ์ด์ง์ ์์.
ํด๋์ค์ ์ฐจ์ด๋ ์ธ์คํด์ค๋ฅผ ์๋ก ์์ฑํ์ง ์์
โ๏ธ ์ธํฐํ์ด์ค ์์ฑ
name๊ณผ age๋ฅผ ๊ฐ์ง PersonInterface๋ฅผ ์์ฑํ๋ค
person2๋ฅผ ๊ฐ์ง๋ ํจ์๋ PersonInterfaceํ์ ๋ง ๋ฐ์ ์ ์๋ค.
๊ทธ๋ฆฌํ์ฌ personObject๋ name,age๋ฅผ ๊ฐ์ง ๊ฐ์ฒด๋ฅผ person2ํจ์์ ๋งค๊ฐ๋ณ์๋ก ๋ฃ์ด์ฃผ์๋ค.
์ด๋ฌํ ๋ฐฉ์์ผ๋ก ๊ฐ์ฒด์ ํ์ ์ ์ถ๋ก ํ ์ ์๋ค.
// ์ธํฐํ์ด์ค ์์ฑ
interface PersonInterface{
name:string,
age:number
}
function person2(person:PersonInterface){
console.log(person.name,person.age);
}
const personObject = {
name:"์ต๋๊ฑด",
age:32
}
person2(personObject)
๐ ์ธํฐํ์ด์ค ์ ํ์ ํ๋กํผํฐ
๋ชจ๋ ์ธํฐํ์ด์ค์ ํ๋กํผํฐ๊ฐ ๊ผญ ํ์์ผ ํ์๋ ์๋ค. ํ์๊ฐ ์๋ ๊ฒฝ์ฐ๋ ?๋ฅผ ํตํด ์ ํ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋๋ก ํ๋ค.
//์ ํ์ ํ๋กํผํฐ
interface PersonInterface2{
name?:string,
age?:number
}
function person3(person:PersonInterface2): {fullName:String}{
const newPerson = {
fullName:""
}
if(person.name){
newPerson.fullName += person.name
}
if(person.age){
newPerson.fullName += "/"+person.age
}
return newPerson
}
person3({
age:11
})
๐ ํจ์ ํ์
๋งค๊ฐ๋ณ์๊ฐ ์๋ ํจ์๋ ํ์ ์ ์ ํ ์๊ฐ ์๋ค.
//ํจ์ํ์
interface SearchFunc{
(source:string,subString:string):boolean
}
let mySearchFunc:SearchFunc
mySearchFunc = function(source:string,subString:string){
let result = source.search(subString)
return result > -1
}
// ํจ์ํ์
2
interface searchFunc2{
(num1:number,num2:number):boolean
}
const diffNumber:searchFunc2 =function(a,b){
return a>b;
}
diffNumber(1,2)