๋ฐ์ํ
๐ ์ ๋ค๋ฆญ์ด ๋ฌด์์ธ๊ฐ
์ ๋ค๋ฆญ์ ๋ค์ํ ํ์ ์ ํ์ฉํจ์ผ๋ก์จ, ์ฌํ์ฉํ๋๋ฐ ์ ์ฉํ ๊ธฐ๋ฅ์ด๋ค
๐ฅ ์ ๋ค๋ฆญ ์ฌ์ฉ์์
์ค๋ณต์ด ๋๋ ๊ฒฝ์ฐ
/*
์ค๋ณต์ด๋๋ ๊ฒฝ์ฐ
*/
// Person1๊ณผ Person2์ ๊ฒฝ์ฐ name,age ์ ์ธํ๊ณ ๊ฐ์ด ๋๊ฐ๋ค
interface Person1 {
type: "human";
race: "yello";
name: "TEST1";
age: 32;
}
interface Person2 {
type: "human";
race: "yello";
name: "TEST2";
age: 28;
}
//์ ๋ค๋ฆญ์ ํตํด ์ค๋ณต ์ ๊ฑฐ
interface PersonGenric<N, A> {
type: "human";
race: "yello";
name: N;
age: A;
}
interface Person3 extends PersonGenric<"choi", 32> {}
interface Person4 extends PersonGenric<"choi2", 28> {}
ํจ์ ํํ์,์ ์ธ๋ฌธ
/*
ํจ์ ์ ์ธ๋ฌธ,ํํ์
*/
//ํจ์ ํํ์
const personFactoryE = <N, A>(name: N, age: A) => ({
type: "human",
race: "yellow",
name,
age,
});
//ํจ์ ์ ์ธ๋ฌธ
function personFactoryD<N, A>(name: N, age: A) {
return {
type: "human",
race: "yellow",
name,
age,
};
}
const p0 = personFactoryE<string,number>("choi",32)
//ํ์
์ถ๋ก ๋ ๊ฐ๋ฅํ๋ค
const p1 = personFactoryE("choi", 32)
const p2 = personFactoryD("choi1", 33);
ํด๋์ค์์ ์ฌ์ฉ
/*
ํด๋์ค์์ ์ฌ์ฉ ๊ฐ๋ฅ
*/
class Person<N, A> {
name: N;
age: A;
constructor(name: N, age: A) {
this.name = name;
this.age = age;
}
method<B>(param: B) {}
}
const classP = new Person<string, number>("choi", 32);
classP.method<string>("1")
Promise ์ ๋ค๋ฆญ
interface Data<T>{
resultCd: string,
data:T
}
interface Response<T>{
status: number,
data:Data<T>
}
interface ResultData{
list:string[]
}
function fn() {
return new Promise<Response<ResultData>>((reolve) => reolve({
// Response Interface (status:number,data:Data<T>)
status: 200,
// Data Interface (resultCd:string,data:T)
data: {
resultCd: '0000',
// ResultData Interface (list:string[])
data: {
list:['1']
}
}
}))
}
โ๏ธ react + typescript์์ ์ ๋ค๋ฆญ ์ฌ์ฉ
Response<T>์ผ๋ก ์ ์ธํจ์ผ๋ก์จ, T์ ๋ค์ํ ํํ๊ฐ ๋ค์ด๊ฐ ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ ํ์ฌ๋ T์ Data ์ธํฐํ์ด์ค๊ฐ ๋ค์ด๊ฐ๋๋ก ํ์๋ค.
์๋์ ๊ฐ์ด ์์ฑํ ๊ฒฝ์ฐ ๋ค์ํ ํํ์ ๊ฐ์ ๋ฐ์ ์ ์์ผ๋ฏ๋ก, ํ์ฅ์ฑ์ ์ฉ์ดํ๋ค
// loginApi.ts
// Response ์ ๋ค๋ฆญ
interface Response<T> {
status: number
data: T
}
// ์ ๋ค๋ฆญ ํ์
์ ์ฐ์ผ ์ธํฐํ์ด์ค
interface Data {
resultCd: string
resultMsg: string
}
// ๋ก๊ทธ์ธ ์์ฒญ ํ์
interface LoginRequest {
id: string
pw: string
}
// Promise์์ returnํ ๋ Responseํํ๋ก ๋ฆฌํด ๋๋ฉฐ, ๊ทธ ์ค์ Response<T>์ T์ ๋งคํ๋๋๊ฒ์ด
// Data ์ธํฐํ์ด์ค์ด๋ค
const loginApi = (param: LoginRequest): Promise<Response<Data>> => {
const successMsg: Data = {
resultCd: '0000',
resultMsg: '๋ก๊ทธ์ธ์ฑ๊ณต',
}
const failMsg: Data = {
resultCd: '0001',
resultMsg: '๋ก๊ทธ์ธ์คํจ',
}
return Promise.resolve({
status: 200,
data: param.id === 'chleorjs37@gmail.com' && param.pw === 'chleorjs12@' ? successMsg : failMsg,
})
}
export { loginApi }
ํ๋ฉด์์ loginApiํธ์ถ์ ํตํด vscode์์ res์ ํ์ ์ ๋ฏธ๋ฆฌ ์ ์ ์๋ค. Response<Data>์ผ๋ก ์ถ๋ก ์ด ๊ฐ๋ฅํ๋ฉฐ,
์์ ํ๋๋ฐ ๊ต์ฅํ ์ ์ฉํ๋ค
/* eslint-disable arrow-parens */
import * as styles from './login.module.css'
import Input from '../../components/common/Input/Input'
import Button from '../../components/common/Button/Button'
import { loginApi } from '../../service/api/loginApi'
import { useState } from 'react'
function Login() {
const [userInfo, setUserInfo] = useState({
id: '',
pw: '',
})
function isValidation() {
if (!userInfo.id || !userInfo.pw) return false
return true
}
function idInputChangeEvent(text: string) {
setUserInfo({
...userInfo,
id: text,
})
}
function pwInputChangeEvent(text: string) {
setUserInfo({
...userInfo,
pw: text,
})
}
function onSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault()
if (!isValidation()) {
alert(`์์ด๋ ๋๋ ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์`)
return
}
const param = {
id: userInfo.id,
pw: userInfo.pw,
}
// ๋ก๊ทธ์ธ Api์ res์ ํ์
์ด ์ถ๋ก ์ด ๊ฐ๋ฅํ๋ค. Response<Data>์ด๋ค
loginApi(param).then((res) => {
// Response์ ํ์
์ด๋ค. status:number, data:Data
const { status, data } = res
if (status === 200) {
// Data์ ํํ์ด๋ค. resultCd,resultMsg
const { resultCd, resultMsg } = data
if (resultCd === '0000') {
alert(resultMsg)
} else {
alert(resultMsg)
}
}
})
}
return (
<div className={styles.login_form_container}>
<form className={styles.login_form} onSubmit={onSubmit}>
<h1 className={styles.login_title}>{'๋ก๊ทธ์ธ'}</h1>
<div className={styles.form_group}>
<label className={styles.form_label}>{'ID'}</label>
<Input placeholder={'ID๋ฅผ ์
๋ ฅํด์ฃผ์ธ์'} changeEvent={idInputChangeEvent} />
</div>
<div className={styles.form_group}>
<label className={styles.form_label}>{'PW'}</label>
<Input placeholder={'PW๋ฅผ ์
๋ ฅํด์ฃผ์ธ์'} inputype={'password'} changeEvent={pwInputChangeEvent} />
</div>
<div className={styles.form_btngroup}>
<Button buttontype={'submit'} buttonname={'๋ก๊ทธ์ธ'} />
</div>
</form>
</div>
)
}
export default Login
๋ฐ์ํ
'typescript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ ์คํฌ๋ฆฝํธ ์ ๋ค๋ฆญ ์ ํ (0) | 2025.02.26 |
---|---|
๐ ํ์ ์คํฌ๋ฆฝํธ ํจ์ (0) | 2025.01.18 |
๐ฆถ ํ์ ์คํฌ๋ฆฝํธ ์ธํฐํ์ด์ค (0) | 2025.01.12 |
๐ง ํ์ ์คํฌ๋ฆฝํธ ๊ธฐ๋ณธ ํ์ (0) | 2025.01.03 |
๐ง ์ธํฐํ์ด์ค (1) | 2024.11.17 |
๋๊ธ