Setup
use Create React App
$1
npx create-react-app my-app --template typescript
# or
yarn create react-app my-app --template typescript$1
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
# or
yarn add typescript @types/node @types/react @types/react-dom @types/jest파일들의 확장자를 tsx로 변경하고, development server를 재시작해줍니다.
not use CRA
https://createapp.dev/ 를 이용하면 쉽게 구성할 수 있습니다.
Import React
import * as React from "react";tsconfig.json 파일에 “allowSyntheticDefaultImports”: true 를 추가하면 다음과 같이 사용할 수 있습니다.
import React from "react";Getting Started
type myProps = { message: string }; // interface를 사용할 수도 있습니다.
function App({message}: myProps) {
return (<div>{message}</div>)
}참조
https://create-react-app.dev/docs/adding-typescript/
https://github.com/typescript-cheatsheets/react-typescript-cheatsheet
https://createapp.dev/
type AppProps = {
message: string;
count: number;
disabled: boolean;
/** array of a type! */
names: string[];
/** string literals to specify exact string values, with a union type to join them together */
status: "waiting" | "success";
/** any object as long as you dont use its properties (not common) */
obj: object;
obj2: {}; // almost the same as `object`, exactly the same as `Object`
/** an object with defined properties (preferred) */
obj3: {
id: string;
title: string;
};
/** array of objects! (common) */
objArr: {
id: string;
title: string;
}[];
/** any function as long as you don't invoke it (not recommended) */
onSomething: Function;
/** function that doesn't take or return anything (VERY COMMON) */
onClick: () => void;
/** function with named prop (VERY COMMON) */
onChange: (id: number) => void;
/** alternative function type syntax that takes an event (VERY COMMON) */
onClick(event: React.MouseEvent<HTMLButtonElement>): void;
/** an optional prop (VERY COMMON!) */
optional?: OptionalType;
};- visual hex code backtick
#000000 - visual diff
- 20 test + 20 test - details and summary tag. details
example