1. type
type PeopleType = {
name: string
age: number
}
type WindowStates = "open" | "closed" | "minimized";
type LockStates = "locked" | "unlocked";
type PositiveOddNumbersUnderTen = 1 | 3 | 5 | 7 | 9;
type Dog = {
name: string;
tail?: {
shakeTail?: () => string;
};
hasMaster?: boolean;
};
function sayHi(dog: Dog): string {
return dog.tail?.shakeTail?.();
}
Type์ property๋ฅผ optionalํ๊ฒ ์ง์ ํ ์ ์๋ค.
2. interface
- ์ฃผ๋ก ๊ฐ์ฒด์ value ํ์ ์ ์ ์ํ๋ค.
- ์ธํฐํ์ด์ค ์ด๋ฆ์์ ๋๋ฌธ์๋ก ์ด๋ค.
- type๊ณผ ๋ค๋ฅด๊ฒ ์ ์ธ์ ํ์ฅ(๊ฐ์ ์ด๋ฆ์ผ๋ก ํ์ฅ ์ ์ธ)์ด ๋๋ค๋ ํน์ง์ด ์๋ค.
interface User {
name: string;
id: number;
addComment: (comment:string) => string;
}
์ ์ํ interface์ ํ์ ์ user ์ ์ ์ฉํ๋ค.
const user: User = {
name: "Hayes",
id: 0,
addComment: (comment) => `This is ${comment}`
};
์ธํฐํ์ด์ค ํ์ฅ ์์
interface ButtonInterface {
readonly _type:string;
width?:number;
height?:number;
onInit?():void;
onClick():void;
}
// ButtonInterface๋ฅผ ํ์ฅํ๋ ToggleButtonInterface
interface ToggleButtonInterface extends ButtonInterface {
toggle():void;
onToggled?():void;
}
// ButtonInterface๋ฅผ ํ์ฅํ๋ CounterButtonInterface
interface CounterButtonInterface extends ButtonInterface {
increase():void;
decrease():void;
onIncreased?():void;
onDecreased?():void;
}
3. Generic
Generic์ด๋ ๋ฐ์ดํฐ์ ํ์ ์ ์ผ๋ฐํํ๋ค(generalize)ํ๋ค๋ ๊ฒ์ ๋ปํ๋ค. ์ฌ๋ฌ ์ข ๋ฅ์ ํ์ ์ ๋ํด ํธํ์ ๋ง์ถฐ์ผ ํ๋ ์ํฉ์์ ์ฌ์ฉํ๋ ๋ฌธ๋ฒ์ด๋ค. Generic์ ์๋ฃํ์ ์ ํ์ง ์๊ณ ์ฌ๋ฌ ํ์ ์ ์ฌ์ฉํ ์ ์๊ฒ ํด์ค๋ค. ์ฆ, ์ ์ธ ์์ ์ด ์๋๋ผ ์์ฑ ์์ ์ ํ์ ์ ๋ช ์ํ์ฌ ํ๋์ ํ์ ๋ง์ด ์๋ ๋ค์ํ ํ์ ์ ์ฌ์ฉํ ์ ์๋๋ก ํ๋ ๊ธฐ๋ฒ์ด๋ค. ํ๋ฒ์ ์ ์ธ์ผ๋ก ๋ค์ํ ํ์ ์ '์ฌ์ฌ์ฉ'์ด ๊ฐ๋ฅํ๋ค๋ ์ฅ์ ์ด ์๋ค. ์ ๋ค๋ฆญ์ ์ฐ์ง ์์ ๊ฒฝ์ฐ, ๋ถํ์ํ ํ์ ๋ณํ์ ํ๊ธฐ ๋๋ฌธ์ ํ๋ก๊ทธ๋จ์ ์ฑ๋ฅ์ ์ ์ํฅ์ ๋ฏธ์น๊ธฐ๋ ํ๋๋ฐ, ์ ๋ค๋ฆญ์ ์ฌ์ฉํ๊ฒ๋๋ฉด ๋ฐ๋ก ํ์ ๋ณํ์ ํ ํ์๊ฐ ์์ด์ ํ๋ก๊ทธ๋จ์ ์ฑ๋ฅ์ด ํฅ์๋๋ ์ฅ์ ์ด ์๋ค.
function sort<T>(items:T[]) :T[] {
return items.sort();
}
const nums :number[] = [1,2,3,4];
const chars :string[] = ["a", "b", "c", "d"];
sort<number>(nums);
sort<string>(chars);
generic์ ์ฐ์ง ์๋๋ค๋ฉด,
1) ํ์ ์ ๋ฏธ๋ฆฌ ์ง์ ํ๊ฑฐ๋, 2) any๋ฅผ ์ด์ฉํ์ฌ ๊ตฌํํด์ผ ํ๋ค.
1) ํ์คํ ํ์
์ฒดํฌ๊ฐ ์ด๋ค์ง ์ ์๊ฒ ์ง๋ง ํญ์ number๋ผ๋ ํ์
์ ๋ฐ์์ผํ๋ฏ๋ก ๋ฒ์ฉ์ฑ์ด ๋จ์ด์ง๋ค.
2) any๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ์๋ฃ์ ํ์
์ ์ ํํ ์ ์๊ณ , ์ด function์ ํตํด ์ด๋ค ํ์
์ ๋ฐ์ดํฐ๊ฐ ๋ฆฌํด๋๋์ง ์ ์ ์๋ค.
generic์ ์ด๋ค๋ฉด,
1) ์ ๋ค๋ฆญ์ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ
2) ๋ฐฐ์ด์ ํํ๋ฅผ ๋ฐ์ ๋
3) ๋ ๊ฐ ์ด์์ Generic ์ฌ์ฉํ๊ธฐ
๋ ๋ณ์์ ํ์ ์ด ๋ค๋ฅผ ๊ฒฝ์ฐ ๋ ๊ฐ์ง์ ํ์ ๋ณ์๊ฐ ํ์ํ ๋ ์ฌ์ฉํ๋ค. T ๋ค์์ผ๋ก ์ค๋ ์ํ๋ฒณ์ธ U๋ฅผ ์ฌ์ฉํ๋ค.
4) ๋ ๊ฐ ์ด์์ Generic ์ฌ์ฉํ๊ธฐ
extends์ ์ผํญ์ฐ์ฐ์๋ฅผ ํตํด ์กฐ๊ฑด๋ถ ํ์ ์ผ๋ก๋ ์ ์ํ ์ ์๋ค.
4. ์ฝ๋ ์คํ์ผ(Props-suffix)
When using props we name props usually according to the component we’re writing, with a Props-suffix.
type GreetingProps = {
name: string;
};
function Greeting(props: GreetingProps) {
return <p>Hi {props.name} ๐</p>
}
Destructuring ์ฝ๋ ์คํ์ผ์ ์์ ํ์ฌ ๊ฐ๋ ์ฑ์ ๋์ฑ ๋์ธ๋ค.
function Greeting({ name }: GreetingProps) {
return <p>Hi {name} ๐</p>;
}
'๐ Front > ๐ซ Typescript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Typescript] ์ ํธ๋ฆฌํฐ ํ์ 12๊ฐ์ง ์ฌ์ฉํ๊ธฐ (0) | 2022.03.22 |
---|---|
[React+Typescript] React.FC๋ฅผ ๊ตณ์ด ์ฌ์ฉํ์ง ์์๋ ๋๋ ์ด์ (0) | 2021.09.06 |
Typescript ์์ฑ์, public, private (0) | 2021.09.06 |
Typescript: ์ค๊ธ(Type Aliases/Type Guard) (0) | 2021.09.06 |
[Typescript] ์ถ๊ฐ ์ ๊ณต ํ์ (tuple, enum, any, void, never) (0) | 2021.09.06 |