Typescript(1)
๐์์ํ๋ฉฐ
Next.js + Typescript ์ธ๊ฐ์ ๋ฃ๋ ์ค ์ ์๋๊ป์
(์ ๋ค๋ฆญ์ ์ฌ์ฉํ์๋ฉด์) ์ด ๋ถ๋ถ์ ์กฐ๊ธ ์ด๋ ค์ธ ์ ์์ด์.
๋ผ๊ณ ๋ง์ํ์ จ๋๋ฐ.. ๊ทธ ๋ง์ ๊ทธ๋๋ก์๋ค. ์ ์๋๊ป์ ์์ฑํ์๋ ๋ฌธ๋ฒ์ด ํ์ ์คํฌ๋ฆฝํธ ์ ๋ค๋ฆญ์ธ ๊ฑด ์๊ณ ์์ง๋ง ๊ทธ๊ฑธ ๋ด๊ฐ ์์ ์์ฌ๋ก ์ธ ์ ์๋ ๊ฐ๋ ์๋ฌด๋๋ ๋ค๋ฅธ ๋ฌธ์ ๋ค ๋ณด๋โฆ ๐ฅ ๊ทธ๋์, ์ด๋ฒ์ ํ์คํ๊ฒ! ์์ฃผ ์ฌ์ฉํด์๋ ๊ฒ๊ณผ ์ถ๊ฐ๋ก ๊ณต๋ถํ ํ์ ์คํฌ๋ฆฝํธ ๋ด์ฉ์ ์ ๋ฆฌํ๊ณ ์ ํ๋ค.
์ด ๊ธ์ ์๋์ ๊ฐ์ด ์ด์ด์ง๋๋ค.
โ typescript ์ฌ์ฉํ๊ธฐ
๋จผ์ typescript๋ฅผ react๋ next์์ ์ฌ์ฉํ ๋๋ ์ด๋ป๊ฒ ํด์ผํ ๊น?
- cra๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ
1
npx create-react-app [ํ๋ก์ ํธ ๋ช
] --template typescript
- vite๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ
1
yarn create vite [ํ๋ก์ ํธ ๋ช
] --template react-ts
- next๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ
1
npx create-next-app@latest
next.js๋ ์ค์น ๋น์์ typescript ์ฌ์ฉ์ฌ๋ถ๋ฅผ ๋ฌผ์ด๋ณด๊ธฐ ๋๋ฌธ์ ์ฌ์ฉํ๋ค๊ณ ๋ตํด์ฃผ๋ฉด ๋๋ค.
โ ์์ํ์
๊ธฐ๋ณธ์ ์ธ JS์ ์์ํ์ ์ ๋ค์๊ณผ ๊ฐ์ด ํ์ ์ ์ ์ํ๋ค.
1
2
3
4
5
let name: string = "์งฑ๊ตฌ"
let age: number = 5
let isMarried: boolean = false
let isNull: null = null
let isUndefined: undefined = undefined
โ ๋ฐฐ์ด๊ณผ ๊ฐ์ฒด
๋ฐฐ์ด ์ ์ ์ ์์์ ์ดํด๋ณธ ํ์
๊ณผ ํจ๊ป []
๊ธฐํธ๋ฅผ ์์ฑํด์ฃผ๋ฉด ๋๋ค. ํด๋น ํ์
์ผ๋ก ์ด๋ฃจ์ด์ง ๋ฐฐ์ด์์ ๋ํ๋ธ๋ค.
1
2
let name: string[] = ["์งฑ๊ตฌ", "์ ๋ฆฌ", " ๋งน๊ตฌ"]
let age: number[] = [5, 4, 6]
๋ง์ฝ ๋ฐฐ์ด์์ ํ์
์ด ์ฌ๋ฌ๊ฐ๋ผ๋ฉด (ํ์
1|ํ์
2)[]
๊ณผ ๊ฐ์ด ์์ฑํ๋ค.
1
let group: (string | number | boolean)[] = ["์งฑ๊ตฌ", "์ ๋ฆฌ", 5, " ๋งน๊ตฌ", false]
โ ํจ์
ํจ์์์ ์ง์ ํด์ฃผ์ด์ผ ํ๋ ํ์ ์ ํฌ๊ฒ ๋ ๊ฐ์ง๋ก ๋ณผ ์ ์๋ค. ๋งค๊ฐ๋ณ์์ ํ์ ๊ณผ, ํจ์๊ฐ ๋ฐํํ๋ ๊ฐ์ ํ์ ์ด๋ค.
โก๏ธ๋ฐํ ๊ฐ์ด ์์ ๋
๋ฐํ๊ฐ(return ๊ฐ)์ด ์์ ๊ฒฝ์ฐ return ํ๋ ๊ฐ์ type์ ์์ฑํด์ฃผ๋ฉด ๋๋ค.
1
2
3
function hello(a: number, b: number): number {
return a + b
}
โก๏ธ๋ฐํ ๊ฐ์ด ์์ ๋
๋ฐํ๊ฐ(return ๊ฐ)์ด ์๋ ๊ฒฝ์ฐ type์ void๋ก ์์ฑํด์ฃผ๋ฉด ๋๋ค.
1
2
3
function hello(name: string, age: number): void {
console.log("๋ด ์ด๋ฆ์" + name + "๋์ด๋" + age + "์
๋๋ค")
}
โ Type Aliases
์์์ ์ดํด๋ณธ ๊ฒ ์ฒ๋ผ ์ ์ํ ๋ ํ์ํ type์ ํ ๋นํ ์๋ ์์ง๋ง ์ฌ์ฌ์ฉ์ฑ์ด ๋จ์ด์ง๋ค๋ ๋ฌธ์ ๊ฐ ์๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ํ์ ์ ์ ์ํ๊ณ ์ด๋ฆ์ ๋ถ์ผ ์ ์๋ ๊ธฐ๋ฅ์ด ์กด์ฌํ๋ค.
1
2
3
4
type User = { name: string; age: number }
let shin: User = { name: "์งฑ๊ตฌ", age: 5 }
let han: User = { name: "์ ๋ฆฌ", age: 5 }
โก๏ธ | (union)
(union)์ ์ด์ฉํด ์ฌ๋ฌ ํ์
์ค ํ ๊ฐ์ง๋ฅผ ๋ํ๋ผ ์ ์๋ค. ์๋ ์์ ์์ User ๋ string , number ๊ฐ ์ค ํ๋๋ฅผ ๊ฐ์ง๋ค๋ ๋ป์ด๋ฉฐ, Obj ๋ name: string ์ธ ๊ฐ์ฒด์ด๊ฑฐ๋ age:number ๊ฐ์ ๊ฐ์ง๋ ๊ฐ์ฒด๋ผ๋ ๋ป์ด๋ค. |
1
2
3
4
5
6
7
8
9
10
11
type User = string | number
type Obj = { name: string } | { age: number }
let user1:User = "์งฑ๊ตฌ"โญ //string ๊ฐ ๊ฐ๋ฅ
let user2:User = 5โญ //number ๊ฐ ๊ฐ๋ฅ
let user3:User = falseโ //boolean ๊ฐ ๋ถ๊ฐ๋ฅ
let user4:Obj = { name: "์งฑ๊ตฌ" }โญ
let user5:Obj = { age: 5 }โญ
let user6:Obj = { isMarried: false }โ
//isMarried๊ฐ key ๊ฐ์ธ ๊ฐ์ฒด ํ์
์ ๊ฐ์ง๊ณ ์์ง ์์. ๋ถ๊ฐ๋ฅ
โก๏ธ & (intersection)
& (intersection)์ ์ด์ฉํด ์ฌ๋ฌ ํ์ ์ ๊ฐ์ง๋ ์๋ก์ด ํ์ ์ ๋ง๋ค ์ ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
type Name = { name: string }
type Age = { age: number }
type User = Name & Age
let user1: User = {โญ
name: "์งฑ๊ตฌ",
age: 5,
}
let user2: User = {โ //age ๋๋ฝ
name: "์งฑ๊ตฌ",
}
let user3: User = {โ//name ๋๋ฝ
age: 5,
}
โ interface
interface๋ ์ฃผ๋ก ๊ฐ์ฒด์ ํ์
์ ์ง์ ํ ๋ ์ฌ์ฉํ๋ค. type
ํค์๋๋ฅผ ์ธ ๋์ ์ฐจ์ด์ ์ =
์์ด ๋ฐ๋ก {}
์์ ํ์
์ ์์ฑํ๋ค๋ ๊ฒ์ด๋ค!
1
2
3
4
5
6
7
8
9
10
11
12
interface User {
name: string
age: number
isMarried: boolean
}
//์ฌ์ฉ์
const shin: User = {
name: "์งฑ๊ตฌ",
age: 5,
isMarried: false,
}
โก๏ธinterface extends
interface๋ extends ๊ตฌ๋ฌธ์ ์ด์ฉํด ๊ธฐ์กด์ ์ ์๋ ํ์ ์ ์์๋ฐ๊ณ , ํ์ฅํ ์ ์๋ค!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface Name {
name: string
}
interface User extends Name { ๐
age: number
isMarried: boolean
}
let shin: User = {
name: "์งฑ๊ตฌ",
age: 5,
isMarried: false,
}
โก๏ธ? (optional)
? (optional) ํค์๋๋ฅผ ์ฌ์ฉํด ์ ํ์ ์ผ๋ก ์ฌ์ฉ๋๋ ์์ฑ์ ๋ง๋ค ์ ์๋ค. ์๋ฅผ ๋ค์ด ์ฌ๋ฌ ๊ฐ์ฒด๋ฅผ ๋ง๋๋๋ฐ ๊ฐ๊ฐ์ ๊ฐ์ฒด์ name, age๋ฅผ ๋ชจ๋ ๊ฐ์ง๊ณ ์๊ฑฐ๋ ํ ๊ฐ์ง๋ง ๊ฐ์ง๋ค๊ณ ์๊ฐํด๋ณด์.
name,age๋ฅผ ๋ชจ๋๊ฐ์ง๊ณ ์๋ ํ์ , name ํ์ , age ํ์ ์ ํ๋์ฉ ์ง์ ํ๊ธฐ์ ๋๋ฌด ๋ฒ๊ฑฐ๋กญ๋ค.๐ค ์ด๋ฅผ ํด๊ฒฐํ ์ ์๋ ๋ฐฉ๋ฒ์ธ ๊ฒ์ด๋ค.
1
2
3
4
5
6
7
8
interface User {
name: string
age?: number๐ // age๋ ์ ํ ์์ฑ์ด๋ค!
}
let shin: User = { name: "์งฑ๊ตฌ" }โญ
let han: User = { name: "์ ๋ฆฌ", age: 5 }โญ
let lee: User = { age: 5 }โ //name์ ํ์๋ค.
โ Enum
Enum์ โ์ด๊ฑฐํโ ์ผ๋ก ์๋ฐ์คํฌ๋ฆฝํธ์๋ ์กด์ฌํ์ง์์ง๋ง, ํ์ ์คํฌ๋ฆฝํธ์์ ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ด๋ค. Enum์ ์ด์ฉํด ํน์ ์งํฉ ๊ฐ๋ค์ ์ ์ํ๋ค๊ณ ๋ณผ ์ ์๋ค.
1
2
3
4
5
6
7
enum User {
JjangGu,
Yuri,
}
let member: User = User.JJangGuโญ
let member: User = User.Yuriโญ
์ฆ ์๋์ ๊ฒฝ์ฐ, Hun
์ enum ํค์๋๋ฅผ ํตํ ์งํฉ์ ์ ์๋์ด ์์ง ์์ผ๋ฏ๋ก, ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
1
let member: User = User.Hunโ
โ ํ์ ๋ฐฉ์ด
โ ํ์ ๋จ์ธ
๊ธฐ๋ณธ์ ์ผ๋ก ํ์ ์คํฌ๋ฆฝํธ๋ โํ์ ์ถ๋ก โ์ ์ํด ํ์ ์ ๊ฒฐ์ ํ ์ ์์ง๋ง ๊ฐ๋ฐ์๊ฐ ํด๋น ๋ฐ์ดํฐ์ ๋ํ ํ์ ์ ๋จ์ธํ ์ ์๋ค. ์ด ๋ ๋จ์ธ์ ํ์คํ ์ํฉ์์ ์ฌ์ฉํ๋ ๊ฒ์ ์ฃผ์ํ๋ค.
ํ์
๋จ์ด๋ as
๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
์๋ฅผ ๋ค์ด ์๋์ ๊ฐ์ด getElementById
๋ฅผ ์ฌ์ฉํด ๊ฐ์ ๋ถ๋ฌ์จ๋ค๊ณ ๊ฐ์ ํ์. ํ์
์คํฌ๋ฆฝํธ๋ getElementById
๋ก ๋ถ๋ฌ์ฌ ๊ฒฝ์ฐ ํ์
์ HTMLElement | null
๋ก ์ถ๋ก ํ๋ค. ํ์ง๋ง ๊ฐ๋ฐ์๋ ํด๋น ๊ฐ์ฒด์ ํ์
์ด HTMLCanvasElement
์์ ์๊ณ ์๋ค.
1
const myCanvas = document.getElementById("main_canvas")
์ด ๊ฒฝ์ฐ asํค์๋๋ฅผ ์ฌ์ฉํ ํ์ ๋จ์ธ์ผ๋ก ํ์ ์คํฌ๋ฆฝํธ์๊ฒ ๋ช ์์ ์ผ๋ก ํ์ ์ ์ ํด์ค ์ ์๋ ๊ฒ์ด๋ค.
1
const myCanvas = document.getElementById("main_canvas") as HTMLCanvasElement
โ ! (non-Null Assertion)
! (non-Null Assertion)์ ์๋ฏธ ๊ทธ๋๋ก, null์ด ์๋์ ๋จ์ธ ํ๋ ๋ฐฉ๋ฒ์ด๋ค. typescript์์ ์ปดํ์ผ ์ต์ ์ธ strictNullChecks๋ฅผ ์ผ๋ฉด ๊ฐ์ด null์ด๊ฑฐ๋ ์ ์๋์ง ์์ ๊ฒฝ์ฐ, ํด๋น ๊ฐ์ ๋ฉ์๋๋ ์์ฑ์ ์ฌ์ฉํ๊ธฐ ์ ์ ํด๋น ๊ฐ์ ํ ์คํธํด์ผ ํ๋ค.
์๋ ์์ ์์ x
๋ optional๋ก ์ค์ ๋์๋ค. ์ฆ, x์ ํ์
์ number ๋๋ null์ผ ์ ์๋ค๋ ๊ฒ์ด๋ค.
1
2
3
4
function liveDangerously(x?: number | null) {
// ์๋์์ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ค.
console.log(x!.toFixed())
}
๋ฐ๋ผ์, console.log(x.toFixed())
๋ฅผ ์๋ํ๋ฉด, typescript๋
x๊ฐ null์ผ์๋ ์๊ฒ ๋๋ฐ?
๋ผ๊ณ ์๊ฐํ๋ฉฐ ์๋ฌ๋ฅผ ๋ฐ์์ํจ๋ค. ๊ทธ๋ฌ๋ ๊ฐ๋ฐ์ ์
์ฅ์์, x๊ฐ null์ด ์๋์ ํ์ ํ๋ค๋ฉด, ํจ์ ๋ด๋ถ์์ x!.toFixed()
์ฒ๋ผ x๊ฐ null์ผ ๊ฐ๋ฅ์ฑ์ด ์๋ค๊ณ ๋จ์ธํด์ค ์ ์๊ณ , ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๊ฒ ๋๋ค.
โ d.ts
d.ts ํ์ผ์ โ์ ์ธ ํ์ผ(Declaration file)โ ์ด๋ผ๊ณ ํ๋ค. ์ต๊ทผ์ ๋ฑ์ฅํ๋ ๋๋ถ๋ถ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ typescript๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก Type์ ์ ๊ณตํ์ง๋ง, ์ค๋ ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์๋ ์ ๊ณตํ์ง ์์ ์ ์๋ค.๐ค
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ํด, d.ts
ํ์ผ์ ์ด์ฉํด ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํ์
์ ๋ณด๋ฅผ ์ง์ ํด์ค ์ ์๋ค.
๐ฉ๋ง๋ฌด๋ฆฌ
์์ฃผ ์ฌ์ฉํ๋ ํ์ ๋ค์, ์ด๋ ๊ฒ ์ ๋ฆฌํ๋ฉด์ ํ ๋ฒ ๋ ์ดํดํ ์ ์์๋ ๊ฒ ๊ฐ๋ค!
์ ๋ค๋ฆญ๊ณผ ์ ํธ๋ฆฌํฐ ํ์ ์ด ๋จ์๋๋ฐ, ๊ธ์ด ๊ธธ์ด์ง ๊ฒ ๊ฐ์์ ๋ค์ ํฌ์คํธ์์ ์ด์ด์ ์ ๋ฆฌํ๊ณ ์ ํ๋ค.๐
๐๏ธ์ฐธ๊ณ ์ฌ์ดํธ
-https://www.typescriptlang.org/ko/docs/handbook/declaration-files/dts-from-js.html