๐Ÿ“Œ props๋กœ ๋„˜๊ฒจ์ฃผ๋Š” data๊ฐ€ ์–ด๋–ค ํƒ€์ž…์ธ์ง€ ์ง€์ •๋˜์ง€ ์•Š์•˜์„๋•Œ

Untitled

import React from "react";
import logo from "./logo.svg";
import "./App.css";
import Store from "./Store";

let data = {
	name: "type Store",
  category: "Korean Food",
  address: {
    city: "Seoul",
    detail: "someWhere",
    zipCode: 12345678,
   },
    menu: [
      { name: "rice", price: 1000 },
      { name: "soup", price: 2000 },
    ],
  };

const App: React.FC = () => {
  return (
    <div className="App">
      <Store **data={data}**/>
    </div>
  );
};

export default App;

src - model - ํŒŒ์ผ๋ช….ts ์ƒ์„ฑ

App.tsx์— ์žˆ๋Š” data์˜ ํƒ€์ž…๋“ค์„ ๋ณ„๋„๋กœ ๋นผ์™€์„œ type์„ ์ง€์ •ํ•ด์ค€๋‹ค.

ํƒ€์ž…์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” type๊ณผ interface ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๋‘˜์˜ ์ฐจ์ด์ ์€ ํฌ๊ฒŒ ์—†์œผ๋‚˜, method์™€ syntax์˜ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

โœ๏ธ

let data = {
  name: "type Store",
  category: "Korean Food",
  address: {
    city: "Seoul",
    detail: "someWhere",
    zipCode: 12345678,
  },
  menu: [
    { name: "rice", price: 1000 },
    { name: "soup", price: 2000 },
  ],
};

Type

type์œผ๋กœ ์ง€์ •ํ•ด๋ณด๊ธฐ

๋‚ด๊ฐ€ ์ดํ•ดํ•œ๋ฐ”๋กœ๋Š” ์–ด๋–ค data๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๊ฐ์ฒด์˜ ๊ตฌ์กฐ๋ฅผ ๋ฏธ๋ฆฌ ์žก์•„๋‘๊ณ  ๊ทธ ์•ˆ์— ๋ฐ›์•„์˜ค๋Š” data๋“ค์˜ type์„ ์ •ํ•ด๋‘๋Š”๊ฒƒ ๊ฐ™๋‹ค.

export type Restaurant = {
  name: string;
  category: string;
  address: {
    city: string;
    detail: string;
    zipCode: number;
  };
  menu: {
    name: string;
    price: number;
  }[];
};

โœ๏ธ type์„ ์กฐ๊ธˆ ๋” ์žฌ์‚ฌ์šฉ์„ฑ ๋†’ํžˆ๋„๋ก ๋งŒ๋“ค์–ด๋ณด๊ธฐ

export type Restaurant = {
  name: string;
  category: string;
  address: Address;
  menu: Menu[];
};

export type Address = {
  city: string;
  detail: string;
  zipCode: number;
};

export type Menu = {
  name: string;
  price: number;
};
**import { Restaurant } from "./model/restaurant";**
.
.
let **data:Restaurant** = {
.
.
};