ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ๋ ์ด์
1. IDE(Integrated Development Environment)๋ฅผ ๋์ฑ ๋ ์ ๊ทน์ ์ผ๋ก ํ์ฉ (์๋์์ฑ, ํ์ ํ์ธ)
2. ์ค์ ๋ฐฉ์ง
1. ํ์ ์คํฌ๋ฆฝํธ ์ฐ์ต
$ mkdir ts-practice # ts-practice ๋ผ๋ ๋๋ ํฐ๋ฆฌ ์์ฑ
$ cd ts-practice # ํด๋น ๋๋ ํฐ๋ฆฌ๋ก ์ด๋
$ yarn init -y # ๋๋ npm init -y
์ด๋ ๊ฒ ํ๋ฉด ts-practice ๋๋ ํฐ๋ฆฌ์ package.json ์ด๋ผ๋ ํ์ผ์ด ๋ง๋ค์ด์ง๋๋ค.
ํ์ ์คํฌ๋ฆฝํธ ์ค์ ํ์ผ ์์ฑํ๊ธฐ
$ yarn global add typescript
๊ทธ๋ฆฌ๊ณ ํ๋ก์ ํธ ๋๋ ํฐ๋ฆฌ ์์์ tsc --init ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ๋ฉด tsconfig.json ํ์ผ์ด ์๋์์ฑ๋ฉ๋๋ค.
<tsconfig.json>
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "./dist"
}
}
ํ์ ์คํฌ๋ฆฝํธ ํ์ผ ๋ง๋ค๊ธฐ
<src/practice.ts>
const message: string = 'hello world';
console.log(message);
์ฝ๋๋ฅผ ๋ชจ๋ ์์ฑํ์ จ์ผ๋ฉด ํด๋น ํ๋ก์ ํธ์ ๋๋ ํฐ๋ฆฌ์ ์์นํ ํฐ๋ฏธ๋์์ tsc ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํด๋ณด์ธ์.
๊ทธ๋ฌ๋ฉด dist/practice.js ๊ฒฝ๋ก์ ๋ค์๊ณผ ๊ฐ์ด ํ์ผ์ด ์์ฑ๋ ๊ฒ์ ๋๋ค.
tsc // ์ปดํ์ผ
node dist/practice // ์คํ
<dist/practice.js>
"use strict";
var message = 'hello world';
console.log(message);
๊ธฐ๋ณธ ํ์
<src/practice.ts>
let count = 0; // ์ซ์
count += 1;
count = '๊ฐ์๊ธฐ ๋ถ์๊ธฐ ๋ฌธ์์ด'; // ์ด๋ฌ๋ฉด ์๋ฌ๊ฐ ๋ฉ๋๋ค!
const message: string = 'hello world'; // ๋ฌธ์์ด
const done: boolean = true; // ๋ถ๋ฆฌ์ธ ๊ฐ
const numbers: number[] = [1, 2, 3]; // ์ซ์ ๋ฐฐ์ด
const messages: string[] = ['hello', 'world']; // ๋ฌธ์์ด ๋ฐฐ์ด
messages.push(1); // ์ซ์ ๋ฃ์ผ๋ ค๊ณ ํ๋ฉด.. ์๋๋ค!
let mightBeUndefined: string | undefined = undefined; // string ์ผ์๋ ์๊ณ undefined ์ผ์๋ ์์
let nullableNumber: number | null = null; // number ์ผ์๋ ์๊ณ null ์ผ์๋ ์์
let color: 'red' | 'orange' | 'yellow' = 'red'; // red, orange, yellow ์ค ํ๋์
color = 'yellow';
color = 'green'; // ์๋ฌ ๋ฐ์!
ํจ์์์ ํ์ ์ ์ํ๊ธฐ
<src/practice.ts>
function sum(x: number, y: number): number {
return x + y;
}
sum(1, 2);
function sumArray(numbers: number[]): number {
return numbers.reduce((acc, current) => acc + current, 0);
}
const total = sumArray([1, 2, 3, 4, 5]);
// ๋ฐํX
function returnNothing(): void {
console.log('I am just saying hello world');
}
interface ์ฌ์ฉํด๋ณด๊ธฐ (for ํด๋์ค ๋๋ ๊ฐ์ฒด๋ฅผ ์ํ ํ์ ์ง์ )
ํด๋์ค์์ interface๋ฅผ implementsํ๊ธฐ
<practice.ts>
// Shape ๋ผ๋ interface ๋ฅผ ์ ์ธํฉ๋๋ค.
interface Shape {
getArea(): number; // Shape interface ์๋ getArea ๋ผ๋ ํจ์๊ฐ ๊ผญ ์์ด์ผ ํ๋ฉฐ ํด๋น ํจ์์ ๋ฐํ๊ฐ์ ์ซ์์
๋๋ค.
}
class Circle implements Shape {
// `implements` ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ํด๋์ค๊ฐ Shape interface ์ ์กฐ๊ฑด์ ์ถฉ์กฑํ๊ฒ ๋ค๋ ๊ฒ์ ๋ช
์ํฉ๋๋ค.
radius: number; // ๋ฉค๋ฒ ๋ณ์ radius ๊ฐ์ ์ค์ ํฉ๋๋ค.
constructor(radius: number) {
this.radius = radius;
}
// ๋๋น๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์๋ฅผ ๊ตฌํํฉ๋๋ค.
getArea() {
return this.radius * this.radius * Math.PI;
}
}
class Rectangle implements Shape {
width: number;
height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
getArea() {
return this.width * this.height;
}
}
const shapes: Shape[] = [new Circle(5), new Rectangle(10, 5)];
shapes.forEach(shape => {
console.log(shape.getArea());
});
// Shape ๋ผ๋ interface ๋ฅผ ์ ์ธํฉ๋๋ค.
interface Shape {
getArea(): number; // Shape interface ์๋ getArea ๋ผ๋ ํจ์๊ฐ ๊ผญ ์์ด์ผ ํ๋ฉฐ ํด๋น ํจ์์ ๋ฐํ๊ฐ์ ์ซ์์
๋๋ค.
}
class Circle implements Shape {
// `implements` ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ํด๋์ค๊ฐ Shape interface ์ ์กฐ๊ฑด์ ์ถฉ์กฑํ๊ฒ ๋ค๋ ๊ฒ์ ๋ช
์ํฉ๋๋ค.
constructor(public radius: number) {
this.radius = radius;
}
// ๋๋น๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์๋ฅผ ๊ตฌํํฉ๋๋ค.
getArea() {
return this.radius * this.radius * Math.PI;
}
}
class Rectangle implements Shape {
constructor(private width: number, private height: number) {
this.width = width;
this.height = height;
}
getArea() {
return this.width * this.height;
}
}
const circle = new Circle(5);
const rectangle = new Rectangle(10, 5);
console.log(circle.radius);
console.log(rectangle.width);
const shapes: Shape[] = [new Circle(5), new Rectangle(10, 5)];
shapes.forEach(shape => {
console.log(shape.getArea());
});
tsc // ์ปดํ์ผ
node dist/practice // ์คํ
public์ผ๋ก ์ ์ธ๋ ๊ฐ: ํด๋์ค ์ธ๋ถ์์ ์กฐํ ๊ฐ๋ฅ
private์ผ๋ก ์ ์ธ๋ ๊ฐ: ํด๋์ค ๋ด๋ถ์์๋ง ์กฐํ ๊ฐ๋ฅ
์ผ๋ฐ ๊ฐ์ฒด๋ฅผ interface๋ก ํ์ ์ค์ ํ๊ธฐ
<practice.ts>
interface Person {
name: string;
age?: number; // ๋ฌผ์ํ๊ฐ ๋ค์ด๊ฐ๋ค๋ ๊ฒ์, ์ค์ ์ ํด๋ ๋๊ณ ์ํด๋ ๋๋ ๊ฐ์ด๋ผ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
}
interface Developer {
name: string;
age?: number;
skills: string[];
}
const person: Person = {
name: '๊น์ฌ๋',
age: 20
};
const expert: Developer = {
name: '๊น๊ฐ๋ฐ',
skills: ['javascript', 'react']
};
interface Person {
name: string;
age?: number; // ๋ฌผ์ํ๊ฐ ๋ค์ด๊ฐ๋ค๋ ๊ฒ์, ์ค์ ์ ํด๋ ๋๊ณ ์ํด๋ ๋๋ ๊ฐ์ด๋ผ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
}
interface Developer extends Person {
skills: string[];
}
const person: Person = {
name: '๊น์ฌ๋',
age: 20
};
const expert: Developer = {
name: '๊น๊ฐ๋ฐ',
skills: ['javascript', 'react']
};
const people: Person[] = [person, expert];
Type Alias ์ฌ์ฉํ๊ธฐ
type: ํน์ ํ์ ์ ๋ณ์นญ์ ๋ถ์ด๋ ์ฉ๋๋ก ์ฌ์ฉ => ๊ฐ์ฒด๋ฅผ ์ํ ํ์ ์ ์ค์ ํ ์๋ ์๊ณ , ๋ฐฐ์ด ๋๋ ๊ทธ ์ด๋ค ํ์ ์ด๋ ๋ณ์นญ์ ์ง์ด์ค ์ ์๋ค.
<src/practice.ts>
type Person = {
name: string;
age?: number; // ๋ฌผ์ํ๊ฐ ๋ค์ด๊ฐ๋ค๋ ๊ฒ์, ์ค์ ์ ํด๋ ๋๊ณ ์ํด๋ ๋๋ ๊ฐ์ด๋ผ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
};
// & ๋ Intersection ์ผ๋ก์ ๋๊ฐ ์ด์์ ํ์
๋ค์ ํฉ์ณ์ค๋๋ค.
// ์ฐธ๊ณ : https://www.typescriptlang.org/docs/handbook/advanced-types.html#intersection-types
type Developer = Person & {
skills: string[];
};
const person: Person = {
name: '๊น์ฌ๋'
};
const expert: Developer = {
name: '๊น๊ฐ๋ฐ',
skills: ['javascript', 'react']
};
type People = Person[]; // Person[] ๋ฅผ ์ด์ ์์ผ๋ก People ์ด๋ผ๋ ํ์
์ผ๋ก ์ฌ์ฉ ํ ์ ์์ต๋๋ค.
const people: People = [person, expert];
type Color = 'red' | 'orange' | 'yellow';
const color: Color = 'red';
const colors: Color[] = ['red', 'orange'];
Generics => ์ฌ๋ฌ ์ข ๋ฅ์ ํ์ ์ ๋ํ์ฌ ํธํ์ ๋ง์ถฐ์ผ ํ๋ ์ํฉ์์ ์ฌ์ฉ
ํจ์์์ Generics ์ฌ์ฉํ๊ธฐ
<src/practice.ts>
function merge<A, B>(a: A, b: B): A & B {
return {
...a,
...b
};
}
const merged = merge({ foo: 1 }, { bar: 1 });
function wrap<T>(param: T) {
return {
param
}
}
const wrapped = wrap(10);
interface์์ Generics ์ฌ์ฉํ๊ธฐ
<src/practice.ts>
interface Items<T> {
list: T[];
}
const items: Items<string> = {
list: ['a', 'b', 'c']
};
type์์ Generics ์ฌ์ฉํ๊ธฐ
<src/practice.ts>
type Items<T> = {
list: T[];
};
const items: Items<string> = {
list: ['a', 'b', 'c']
};
ํด๋์ค์์ Generics ์ฌ์ฉํ๊ธฐ
class Queue<T> {
list: T[] = [];
get length() {
return this.list.length;
}
enqueue(item: T) {
this.list.push(item);
}
dequeue() {
return this.list.shift();
}
}
const queue = new Queue<number>();
queue.enqueue(0);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
console.log(queue.dequeue());
console.log(queue.dequeue());
console.log(queue.dequeue());
console.log(queue.dequeue());
console.log(queue.dequeue());
$ tsc
$ node dist/practice
0
1
2
3
4
2. ๋ฆฌ์กํธ ์ปดํฌ๋ํธ ํ์ ์คํฌ๋ฆฝํธ๋ก ์์ฑํ๊ธฐ
ํ๋ก์ ํธ ์์ฑ
$ npx create-react-app ts-react-tutorial --template typescript
์๋ก์ด ์ปดํฌ๋ํธ ๋ง๋ค๊ธฐ
<src/Greetings.tsx>
import React from 'react';
type GreetingsProps = {
name: string;
mark: string;
optional?: string;
onClick: (name: string) => void; // ์๋ฌด๊ฒ๋ ๋ฆฌํดํ์ง ์๋๋ค๋ ํจ์๋ฅผ ์๋ฏธํฉ๋๋ค.
};
function Greetings({ name, mark, optional, onClick }: GreetingsProps) {
const handleClick = () => onClick(name);
return (
<div>
Hello, {name} {mark}
{optional && <p>{optional}</p>}
<div>
<button onClick={handleClick}>Click Me</button>
</div>
</div>
);
}
Greetings.defaultProps = {
mark: '!'
};
export default Greetings;
<App.tsx>
import React from 'react';
import Greetings from './Greetings';
const App: React.FC = () => {
const onClick = (name: string) => {
console.log(`${name} says hello`);
};
return <Greetings name="Hello" onClick={onClick} />;
};
export default App;
'โ๏ธ > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React ๊ณต๋ถํ๊ธฐ(8) - ์ํ ๊ด๋ฆฌ, Context API of ๋ฆฌ์กํธ ํ์ ์คํฌ๋ฆฝํธ (0) | 2021.11.09 |
---|---|
React ๊ณต๋ถํ๊ธฐ(7) - redux-saga of ๋ฆฌ๋์ค ๋ฏธ๋ค์จ์ด (0) | 2021.11.09 |
React ๊ณต๋ถํ๊ธฐ(7) - redux-thunk of ๋ฆฌ๋์ค ๋ฏธ๋ค์จ์ด (0) | 2021.11.09 |
React ๊ณต๋ถํ๊ธฐ(7) - ๋ฆฌ๋์ค ๋ฏธ๋ค์จ์ด (0) | 2021.11.09 |
React ๊ณต๋ถํ๊ธฐ(6) - ๋ฆฌ๋์ค(Redux) (0) | 2021.11.05 |