250x250
๋ฐ˜์‘ํ˜•
arkhyeon
arkhyeon
arkhyeon
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (88)
    • Spring (5)
    • Java (4)
    • React (25)
      • TypeScript (6)
      • JavaScript (1)
      • Jest (9)
    • NEXT (8)
    • SQL (1)
    • React native (1)
    • CSS (3)
    • Web (1)
    • Git (3)
    • ETC (6)
    • ๋น…๋ฐ์ดํ„ฐDB (8)
    • Docker (4)
    • Tool (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • react loading
  • Spring WebSocket
  • usetransition
  • websocket
  • react jest
  • react usetransition
  • HIVE
  • WSS
  • javasciprt websocket
  • react spring websocket
  • react19
  • javascript wss
  • node WebSocket
  • react
  • react websocket
  • websocket server
  • react typescript
  • kudu
  • docker tomcat
  • jest

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
arkhyeon

arkhyeon

React/TypeScript

React Typescript Interface Tuple Generic

2024. 3. 25. 17:37
728x90
๋ฐ˜์‘ํ˜•
๐Ÿ’ก ๊ณต์‹ ๋ฌธ์„œ์—์„œ Tuple ํƒ€์ž…์€ Interface๊ฐ€ ์•„๋‹Œ Type์œผ๋กœ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉ์„ ์ถ”์ฒœํ•˜์ง€๋งŒ Interface๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๊ถ๊ธˆ์ ์„ ํ•ด์†Œํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•œ๋‹ค.

* Tuple ํƒ€์ž…์€ Type์œผ๋กœ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

Tuple์„ Interface๊ฐ€ ์•„๋‹Œ Type์œผ๋กœ ์„ ์–ธํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

์ธํ„ฐํŽ˜์ด์Šค(Interface)๋Š” ๊ฐ์ฒด ํƒ€์ž…์„ ์ •์˜ ํ˜น์€ ์ƒ์†, ํ™•์žฅํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด ์‚ฌ์šฉ์„ ์ถ”์ฒœํ•˜์ง€๋งŒ ํƒ€์ž…(Type)์ฒ˜๋Ÿผ ์›์‹œ ํƒ€์ž… ๋ฐ ํŠœํ”Œ ํƒ€์ž…์„ ์ •์˜ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•„๋ž˜์˜ ์‚ฌ์šฉ๋ฒ•๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉ์€ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Documentation - Advanced Types

Advanced concepts around types in TypeScript

www.typescriptlang.org

์‚ฌ์šฉ๋ฒ•

ํ•ด๋‹น Hook ์‚ฌ์šฉ ์‹œ ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Array ํƒ€์ž…์„ ํ™•์žฅํ•˜์˜€์œผ๋ฉฐ Generic Type ์‚ฌ์šฉ ์‹œ <T,>๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ Arrow Function์ด ์•„๋‹Œ ๊ธฐ์กด Function ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ ์‹œ <T>๋กœ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค.

import React, {useState} from "react";

interface ReturnType<T> extends Array<T | ((e: React.ChangeEvent<HTMLInputElement>) => void)> {
  0: T;
  1: (e: React.ChangeEvent<HTMLInputElement>) => void;
}

const useInputs = <T,>(initial: T): ReturnType<T> => {
  const [inputs, setInputs] = useState<T>(initial);
  const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const { name, value } = e.target;
    setInputs({ ...inputs, [name]: value });
  }

  return [inputs, onChange];
};

export default useInputs;

import useInputs from "./hook/useInputs.tsx";

interface StateType {
  username: string,
  email: string
}

const Inputs = () => {
  const initState: StateType = {username: "", email: ""};
  const [inputs, onChange] = useInputs(initState);
  const {username, email} = inputs;

  return (
    <div>
      <input type="text" name={"username"} value={username} onChange={onChange}/>
      <input type="text" name={"email"} value={email} onChange={onChange}/>
    </div>
  );
};

export default Inputs;

<T,> ์™œ Arrow function Generic Type์€ ์ฝค๋งˆ ์”€?

์ฝค๋งˆ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋Š” TypeScript์˜ ๋ฌธ๋ฒ•๊ณผ parser์˜ ๋™์ž‘ ๋ฐฉ์‹ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์ฝค๋งˆ๊ฐ€ ์—†์œผ๋ฉด, parser๋Š” ์ œ๋„ค๋ฆญ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๋ฌธ๋ฒ•์œผ๋กœ ํ•ด์„ํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๊ณ , ์ด๋กœ ์ธํ•ด ๋ฌธ๋ฒ• ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Š” ํŠนํžˆ JSX ๋˜๋Š” TSX ํŒŒ์ผ์—์„œ ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ฝค๋งˆ๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ์€ ํŒŒ์„œ์—๊ฒŒ ์ด๊ฒƒ์ด ์ œ๋„ค๋ฆญ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜์ž„์„ ๋ช…ํ™•ํžˆ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

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

React RichTextEditor Image Upload - reactQuill  (0) 2024.03.18
React TypeScript useInfiniteQuery Infinite Scroll  (0) 2024.03.15
React TypeScript Zustand Infinite Scroll with IntersectionObserver  (0) 2024.03.14
React Axios TypeScript with JWT Boilerplate  (0) 2024.03.14
React TypeSciprt - Community Project  (0) 2024.03.13
    'React/TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • React RichTextEditor Image Upload - reactQuill
    • React TypeScript useInfiniteQuery Infinite Scroll
    • React TypeScript Zustand Infinite Scroll with IntersectionObserver
    • React Axios TypeScript with JWT Boilerplate
    arkhyeon
    arkhyeon

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”