티스토리 뷰

 

function object({a,b}:{a:number; b:number}): string {
	return `${a+b}`
}

객체로 넘겨줄때 {객체 명시} : {객체 타입 명시} : return 타입 명시 

 

 

1. 비동기함수에서 사용해 보기

async funciton getPerson() {
	const res = await fetch(`http://localhost.5000/people`);
    if(!res.ok) {
    	throw new Error();
    }
    return res.json();
}

getPerson().then((res) => console.log(res))

 

아래의 객체를 받는다고 했을때

[
	{id:1, age:20, height:180},
   	{id:2, age:28, height:170},
    	{id:3, age:31, height:150},
]

 

interface Person = {
    id:number;
    age: number;
    height: number;
}


async funciton getPerson():Promise<Person[]> {
	const res = await fetch(`http://localhost.5000/people`);
    if(!res.ok) {
    	throw new Error();
    }
    return res.json();
}

getPerson().then((res) => console.log(res))

으로 적용할 수 있다.

 

 

2.제네릭<T>

  const [email, setEmail] = useState<string>("");
  const [password, setPassword] = useState<string>("");

useState뒤에 따라오는 <string> 부분이 <T> 부분인데

[안에 존재하는 값들은 제네릭으로 입력된 타입으로만 받을 수 있다] 정로로만 이해를 하고 넘어가도록 하겠다..

왜냐면 아직 정확하게 뭔소린지 모르겠으므로..