File tree Expand file tree Collapse file tree 2 files changed +55
-32
lines changed Expand file tree Collapse file tree 2 files changed +55
-32
lines changed Original file line number Diff line number Diff line change 1
- import { Add , isAdult } from "./utils/data.interface" ;
2
-
3
- //일반 함수
4
-
5
- // function test(x, y): Add {
6
- // return x + y;
7
- // } //-> 잘못된 일반 함수 선언 방법
8
-
9
- function test ( x : number , y : number ) : number {
10
- return x + y ;
11
- }
12
-
13
- const addTest : Add = test ;
14
- //왜 function에 인터페이스를 지정 할 수 없는가?
15
- //TypeScript에서 함수 타입 인터페이스를 직접 함수 선언에 기입하는 것은 불가능
16
-
17
-
18
- //익명 함수
19
- const add : Add = function ( x , y ) {
20
- return x + y ;
21
- }
22
-
23
- console . log ( add ( 10 , 20 ) ) ;
24
-
25
- //화살표 함수
26
- const isAdult : isAdult = ( age ) => {
27
- return age > 19 ;
1
+ import { Benz , Car } from "./utils/data.interface" ;
2
+
3
+ //클래스 : 객체의 행동과 구조를 정의하기에 '='을 사용한다.
4
+ class Bmw implements Car {
5
+ color = '' ;
6
+ constructor ( color : string ) {
7
+ this . color = color ;
8
+ }
9
+ wheels = 4 ;
10
+ start ( ) {
11
+ console . log ( 'go..' ) ;
12
+ }
28
13
}
29
14
30
- console . log ( isAdult ( 20 ) ) ;
15
+ const b = new Bmw ( 'white' ) ;
16
+ console . log ( b ) ;
17
+ b . start ( ) ;
18
+
19
+ //extends
20
+ //객체 리터럴 : 객체를 리터럴해야하기 때문에 key, value 값으로 정의하게 되고 클래스와 달리 '='를 사용해야 한다.
21
+ const benz : Benz = {
22
+ color : 'black' ,
23
+ wheels : 4 ,
24
+ start ( ) {
25
+ console . log ( 'go...' ) ;
26
+ } ,
27
+ door : 5 ,
28
+ stop ( ) {
29
+ console . log ( 'stop...' ) ;
30
+ } ,
31
+ }
Original file line number Diff line number Diff line change 1
1
type Score = 'A' | 'B' | 'C' | 'F' ;
2
2
3
3
export interface userType {
4
- name : string ,
5
- age : number ,
6
- gender ?: string ,
7
- readonly birthYear : number ,
4
+ name : string ;
5
+ age : number ;
6
+ gender ?: string ;
7
+ readonly birthYear : number ;
8
8
[ grade : number ] : Score ;
9
9
}
10
10
@@ -15,4 +15,26 @@ export interface Add {
15
15
16
16
export interface isAdult {
17
17
( age : number ) : boolean ;
18
+ }
19
+
20
+ // implements
21
+
22
+ export interface Car {
23
+ color : string ;
24
+ wheels : number ;
25
+ start ( ) : void ;
26
+ }
27
+
28
+ export interface Benz extends Car {
29
+ door : number ;
30
+ stop ( ) : void ;
31
+ }
32
+
33
+ interface Toy {
34
+ name : string ;
35
+ }
36
+
37
+ // 동시 확장 방식
38
+ interface ToyCar extends Car , Toy {
39
+ price : number ;
18
40
}
You can’t perform that action at this time.
0 commit comments