Skip to content

Commit de09ca6

Browse files
authored
🔀: Merge pull request #13 from Programming-Contents-List/feature/7
🔀: main브렌치를 최신화 하기위한 merge
2 parents 956ca43 + e727326 commit de09ca6

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

src/index.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
// NonNullable<T> : NonNullable은 Null과 undefined를 제외한 타입을 생성한다.
2-
// -- data.interface 참조
3-
import {
4-
GradeType, ScoreType, User
5-
} from "./utils/data.interface"
1+
// Generic : 클래스나 타입을 재사용하기 위한 문법
2+
// interface와 Generic을 사용해서 활용할 수도 있다.
3+
4+
import { myUser, myCar, myBook, objectType } from "./utils/data.interface";
5+
6+
const user: myUser = { name: "a", age: 10 };
7+
const car: myCar = { name: "bmw", color: "red" };
8+
const book: myBook = { price: 3000 };
9+
10+
// 제네릭 T를 사용해보면 T가 name 속성을 가지고 있다는 보장이 없기 때문에 error가 뜬다. 따라서 name의 타입을 extends를 사용해서 확장 시키면 error가 사라진다.
11+
function showName<T extends objectType>(data: T): string {
12+
return data.name ? data.name : "Undefined";
13+
}
14+
15+
showName(user);
16+
showName(car);
17+
showName(book); //error -> book은 name 객체가 없기 때문에 error가 발생한다. 이를 해결하기 위해서는 조건을 통해서 유연하게 대체를 할수 있다.
18+
// book에 name객체를 삽입하는 방법 외의 선택적 방식으로 동작하게 하려고 했으나 book을 해결하면 나머지가 error가 발생 한다.
19+
// **방법을 한번 찾아보면 좋을 듯 하다.**
20+
//useState같아 보이지 않는가?

src/utils/data.interface.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,26 @@ export interface InterToy {
7676
price: number;
7777
}
7878

79-
export interface User {
80-
id: number;
79+
export interface Mobile<T> {
80+
name: string;
81+
price: number;
82+
option: T;
83+
}
84+
85+
export interface myUser {
8186
name: string;
8287
age: number;
83-
gender: "M" | "F"
8488
}
8589

86-
export interface ScoreTemp {
87-
'1': 'A' | 'B' | 'C' | 'D';
88-
'2': 'A' | 'B' | 'C' | 'D';
89-
'3': 'A' | 'B' | 'C' | 'D';
90-
'4': 'A' | 'B' | 'C' | 'D';
90+
export interface myCar {
91+
name: string;
92+
color: string;
9193
}
9294

93-
export type GradeType = '1' | '2' | '3' | '4';
94-
export type ScoreType = 'A' | 'B' | 'C' | 'D';
95+
export interface myBook {
96+
price: number;
97+
}
9598

96-
//NonNullable
97-
export type T1 = string | null | undefined | void;
98-
//T2는 NonNullable의 따라 T1의 유니온으로 지정된 null과 undefined를 제외하고 string과 void만 남게 된다.
99-
export type T2 = NonNullable<T1>;
99+
export interface objectType extends myBook {
100+
name?: string;
101+
}

0 commit comments

Comments
 (0)