[TypeScript] ๊ฐ์ฒด์ ํ์ interface
- -
๐ฃ ๋ค์ด๊ฐ๊ธฐ ์ ์!
์ด๋ฒ์๋ ๊ฐ์ฒด์ ํ์ 2ํ์ธ ๊ฒ์ธ๊ฐ! ์ง๋๋ฒ ๊ฐ์ฒด์ ํ์ ์ ์ ๋ฆฌํ ๋๋ readonly์ ๋ํด์๋ง ๋ค๋ค์๋๋ฐ, ์ด๋ฒ์๋ interface์ ๋ํด์ ๋ค๋ค๋ณด์
๊ฐ์ฒด์ ํ์ ๊ธฐ๋ณธ๊ฐ๋ ๊ณผ readonly์ ๋ํ ์ ๋ฆฌ๊ธ
[TypeScript] ๊ฐ์ฒด์ ํ์ ๊ณผ readonly
๐ฃ ๋ค์ด๊ฐ๊ธฐ ์ ์!๊ฐ์ฒด์๋ ํ์ ์ด ์กด์ฌํ๋ค!_! ๋น์ฐํ ์ฌ์ค..! ๋๋ฅ! ๊ทธ๋ฌ๋ฉด ์ด๋ฒ์๋ ๊ฐ์ฒด์ ํ์ ๊ณผ readonly์ ๋ํด์ ์ดํด๋ณด์!_! ๊ฐ๋จํ๋ ์งง์ ๊ธ์ด ๋๊ฒ ๋ฐ! ํน์ ์ง๋ ๊ฒ์๋ฌผ์ด ๊ถ๊ธํ๋ค๋ฉด
haileyham.tistory.com
๐ฃ interface
1. ๊ฐ๋
๊ฐ์ฒด์ ํ์ ์ ์ง์ ํ๋ ๊ฒ. TypeScript์์ ๊ฐ์ฒด(Object)์ ๊ตฌ์กฐ๋ฅผ ๋ช ํํ ์ ์ํ์ฌ ํ์ ์ ์ ์ธํ๋ ๋๊ตฌ. ์ฝ๋์์ ํด๋น ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฐ์ฒด๋ ์ธํฐํ์ด์ค์์ ์ ์ํ ์์ฑ๊ณผ ํ์ ์ ์ค์ํ๋ฉฐ, ์ฝ๋์ ๊ฐ๋ ์ฑ์ ๋์ด๊ณ ์ ์ง๋ณด์๋ฅผ ์ฉ์ดํ๊ฒ ํจ.
2. ํน์ง
- interface์ extends / type์ &(intersection) : extends๋ ๋ณต์ฌ, &๋ ์กฐ๊ฑด ๋ง์กฑ
- ์ค๋ณต ์ ์ธ ์ฌ๋ถ : interface ๊ฐ๋ฅ / type ๋ถ๊ฐ๋ฅ : interface์ ๊ฒฝ์ฐ ์ค๋ณต ์ ์ธ์ ํ๊ฒ ๋๋ฉด ํฉ์ณ์ง๋ ๊ฐ๋
- ์ค๋ณต ์์ฑ ์๋ฌ ๋ฐ์ ์ฌ๋ถ : interface์ ๊ฒฝ์ฐ extend ์ค๋ณต ์์ฑ ๋ฐ์์ ๋ฐ๋ก ์๋ฌ ์ฒ๋ฆฌ / type์ ๊ฒฝ์ฐ ๋ฐ๋ก ์๋ฌ์ฒ๋ฆฌ ์ํ๊ณ ์ถํ ์ ์ธํ ํ ๋น๋ ๋ฐ์
3. ์ฝ๋๋ก ์ดํด๋ณด๊ธฐ
interface์ type alias ๋น๊ต
type alias์์ {} ๊ฐ์ฒด๋ก ์ง์ด ๋ฃ๋ ๊ฒ์ interface์์๋ ์๋์ ๊ฐ์ด ํ ์ ์์.
// type alias
type Square = { color: string, width: number };
//์ฌ๊ธฐ์๋ ์์ type๊ณผ ๋์ผ
interface Square {
color: string,
width: number
};
let ๋ค๋ชจ: Square = { color: 'red', width: 100 };
interface
// ์ฌ์ฉ์ ์ธํฐํ์ด์ค ์ ์
interface User {
name: string; // ์ด๋ฆ ์์ฑ์ ๋ฌธ์์ด ํ์
age: number; // ๋์ด ์์ฑ์ ์ซ์ ํ์
isAdmin: boolean; // isAdmin ์์ฑ์ ๋ถ๋ฆฌ์ธ ํ์
}
// ์ฌ์ฉ์ ๊ฐ์ฒด ์์ฑ
let user: User = {
name: "Alice",
age: 30,
isAdmin: true
};
console.log(user.name); // ์ถ๋ ฅ: Alice
๐ฃ interface ์ type alias์ ์ฐจ์ด
0. ๊ธฐ๋ณธ ์ฝ๋
๋ค์์ ๊ธฐ๋ณธ ์ฝ๋๋ก ์ฐจ์ด์ ์ ์ ๋ฆฌํด๋ณด๊ฒ ์!_!
interface Student {
name: string;
}
interface Teacher{
name: string,
age : number
}
let ํ์:Student = { name: 'ham' }
let ์ ์:Teacher = { name : 'hailey', age : 20}
์ฐธ๊ณ ๋ก type alias์ ๋ํด ํท๊ฐ๋ฆฌ๋ค๋ฉด ๋ค์ ์ ๋ฆฌ๊ธ ์ฐธ๊ณ !
[TypeScript] ํ์ ๋ณ์ type alias
๐ฃ ๋ค์ด๊ฐ๊ธฐ ์ ์!์ด๋ฒ์ type alias์ ๋ํด์ ์ดํด๋ณด๊ฒ ๋ค. ์ฌ์ค type alias๋ผ๊ณ ํ๋ฉด ์ผ์? ํ ์๋ ์๋๋ฐ, ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค๋ฉด ๋ค๋ค ๋ง์ด ์ฌ์ฉํ๋ ๊ฒ์ด๋ค. ๊ทธ๋ฅ type์ ๋ณ์์ ๋ด์ ์ฌ
haileyham.tistory.com
1. extends / &(intersection)
interface (extends)
interface์ ์ฅ์ ์ extends๋ก ๋ณต์ฌ ๊ฐ๋ฅํ๋ค๋
์๋์ ์ฝ๋์์ ์ดํด๋ณด๋ฉด, interface Student1 ํ์ ์ extends ๋ก ๋ณต์ฌํ ๊ฒ์ ๋ณผ ์ ์์.
๊ทธ๋ฌ๋ฉด Teacher1์ ํ์ ์ name:string, age:number ๋๋ ๊ฒ!
interface Student1 {
name: string;
}
interface Teacher1 extends Student1{ // extends
age : number
}
let ํ์1:Student = { name: 'ham' }
let ์ ์1:Teacher = { name : 'hailey', age : 20}
type alias ( & : intersection type)
interface์ extends๋ ๋ค๋ฅธ์ : &(intersection type)์ ๋ ํ์ ์ ์ ๋ถ ๋ง์กฑํ๋ ํ์
interface์ extends๋ ๋ณต์ฌ์ ๊ฐ๋
์๋ ์ฝ๋์ ๊ฒฝ์ฐ ์ ์ฒด๋ฅผ ๋ค ๋ง์กฑํ๋ ๊ฐ๋ ์ด๋ผ ์๊ฐํ๋ฉด ๋จ
type Student2 = { name: string };
type Teacher2 = { age: number } & Student2;
2. ์ค๋ณต ์ ์ธ
interface (์ค๋ณต ์ ์ธ ๊ฐ๋ฅ(ํฉ์ณ์ง))
- ์ค๋ณต ์ ์ธ์ด ๊ฐ๋ฅ
- ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฐ์ ๊ฒฝ์ฐ interface ๋ง์ด ์ฌ์ฉ
- ์ถํ ํ์ ์ ์ด๋ค ๊ฒ์ ์ถ๊ฐํ๋ ๊ฒ์ด ์ฉ์ด
- ๋ค๋ฅธ ์ฌ๋์ด ์ด์ฉ ๋ง์ด ํ ๊ฒ ๊ฐ์ object ํ์ ์ ํ ๋ interface ์ฌ์ฉ ๊ฐ๋ฅ
interface StudentTest{
name: string;
}
interface StudentTest{
score: number;
}
let ํ์Test: StudentTest = { name: 'ham', score: 100 };
type alias (์ค๋ณต ์ ์ธ ๋ถ๊ฐ๋ฅ)
๋ค์ ์ฝ๋์ ๊ฒฝ์ฐ error
type StudentTest2 = { name: string };
type StudentTest2 = { score: number }; //error ์ค๋ณต ์ ์ธ ๋ถ๊ฐ๋ฅ
๋ง์ฝ extends ์ฌ์ฉ์ ์ค๋ณต ์์ฑ ๋ฐ์ํ ๊ฒฝ์ฐ?
์๋ฌ๊ฐ ๋จ
interface StudentTest3{
name: string;
}
interface TeacherTest3 extends StudentTest3{
name: string; // ๋๊ฐ์์ ใฑใ
name: number; // extends๋ก ๊ฐ์ ธ์จ๊ฒ์ name : string ๋์ด์๊ธฐ ๋๋ฌธ์ name:number๋ถ๊ฐ
}
type alias ์์ & ์ฌ์ฉํ ๋ ์ค๋ณต ์์ฑ ๋ฐ์ํ๋ฉด?
๋ฏธ๋ฆฌ ์๋ฌ๊ฐ ๋์ง ์์์ ์ฃผ์!
- & : ํฉ์น๋๊ฒ ์๋๋ผ ์ผ,์ค๋ฅธ์ชฝ ๋๋ค ๋ง์กฑํ๋ ํ์ ์ด๊ธฐ ๋๋ฌธ
- name์ด name์ด๊ณ number์ด๊ณ ๋๋ค ๋ง์กฑํ ์๋ ์์
type StudentTest4 = { name: string };
type StudentTest55 = { name: number } & StudentTest4; //์ฌ๊ธฐ์๋ ์๋ฌ ๋ฐ์ ์ํจ
// let ํ์Test1: StudentTest55 = { name: 'ham' }; //์ฌ๊ธฐ name์์ ์๋ฌ ๋ฐ์
interface๋๋ง์ด์ฐ๊ฒ๋์ง~
'FrontEnd > TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TypeScript] class type (0) | 2024.06.20 |
---|---|
[TypeScript] ํจ์์ ํ์ 2ํ!_! (0) | 2024.06.20 |
[TypeScript] Literal Types (0) | 2024.06.20 |
[TypeScript] ํ์ ๋ณ์ type alias (0) | 2024.06.20 |
[TypeScript] ๊ฐ์ฒด์ ํ์ ๊ณผ readonly (0) | 2024.06.20 |
๋น์ ์ด ์ข์ํ ๋งํ ์ฝํ ์ธ
์์คํ ๊ณต๊ฐ ๊ฐ์ฌํฉ๋๋ค