From f6364f3e21b999c40f3ba75ab894cddc59a17b9e Mon Sep 17 00:00:00 2001 From: 4Bee <4bee.code@gmail.com> Date: Tue, 13 Aug 2024 17:14:47 +0900 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=9A=A9:=20=EB=B0=B0=EC=97=B4=20?= =?UTF-8?q?=ED=83=80=EC=9E=85=20=EC=A7=80=EC=A0=95=20=EB=B0=A9=EC=8B=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/data/arr.ts | 13 ++++++++++++- src/index.ts | 17 +++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/data/arr.ts b/src/data/arr.ts index ddb8631..a93cc3b 100644 --- a/src/data/arr.ts +++ b/src/data/arr.ts @@ -1,2 +1,13 @@ export const arrNumber = [2, 6, 3, 6, 5]; -export const arrString = ['1']; \ No newline at end of file +export const arrString = ['1']; + +//이 둘은 같은 타입이다. 타입을 따로 지정하지 않으면 string으로 기본값을 가진다. - 타입 추론 +let element_1: string = 'bmw'; +let element_2 = 'bmw'; + +const element = { + age: 30, + isAdult: true, + a: [1, 2, 3], + a2: [1, 2, 3], +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 71987a5..79c223c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,12 @@ -import { arrNumber, arrString } from "./data/arr"; +//배열 방식 타입 지정 -const showItems = (arr: string[]) => { - arr.forEach(element => console.log(element)); -} -//여기서 함수 자체 파라메터에서도 타입을 개발자가 직접 넣어볼 수 있지 않을까? -showItems(arrString); \ No newline at end of file +let age: number = 30; +let isAdult: boolean = true; +let a: number[] = [1, 2, 3]; +//제네릭을 사용해서 Array의 타입을 지정 +let a2: Array = [1, 2, 3]; + +let week1: string[] = ['mon', 'tue', 'wed']; +let week2: Array = ['mon', 'tue', 'wed']; + +// week1.push(3); //-> error 타입이 일치하지 않음 \ No newline at end of file From 091f3c75e318e7e6248b66f371383f7b41f12964 Mon Sep 17 00:00:00 2001 From: 4Bee <4bee.code@gmail.com> Date: Tue, 13 Aug 2024 17:18:26 +0900 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=9A=A9:=20tuple=20=ED=83=80=EC=9E=85?= =?UTF-8?q?=20=EC=A7=80=EC=A0=95=20=EB=B0=A9=EC=8B=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/data/arr.ts | 5 ++++- src/index.ts | 16 +++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/data/arr.ts b/src/data/arr.ts index a93cc3b..dbf9c79 100644 --- a/src/data/arr.ts +++ b/src/data/arr.ts @@ -10,4 +10,7 @@ const element = { isAdult: true, a: [1, 2, 3], a2: [1, 2, 3], -} \ No newline at end of file +} + +let week1: string[] = ['mon', 'tue', 'wed']; +let week2: Array = ['mon', 'tue', 'wed']; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 79c223c..86b2a62 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,10 @@ -//배열 방식 타입 지정 +// 튜플 타입 지정 방식 -let age: number = 30; -let isAdult: boolean = true; -let a: number[] = [1, 2, 3]; -//제네릭을 사용해서 Array의 타입을 지정 -let a2: Array = [1, 2, 3]; +//첫 번째 인자는 string, 두 번째 인자는 number +let b: [string, number]; -let week1: string[] = ['mon', 'tue', 'wed']; -let week2: Array = ['mon', 'tue', 'wed']; +b = ['z', 3]; +// b = [3, '3']; //-> error -// week1.push(3); //-> error 타입이 일치하지 않음 \ No newline at end of file +b[0].toLowerCase(); +// b[1].toLowerCase(); //-> error: 숫자에는 대/소문자를 지정할 수 없다. From b8229cf52ff85b01cfc77866893910a7eddb102b Mon Sep 17 00:00:00 2001 From: 4Bee <4bee.code@gmail.com> Date: Tue, 13 Aug 2024 17:27:13 +0900 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=9A=A9:=20=ED=95=A8=EC=88=98=20void?= =?UTF-8?q?=EC=99=80=20never=20=EC=82=AC=EC=9A=A9=20=EB=B0=A9=EB=B2=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 86b2a62..cb72a5f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,17 @@ -// 튜플 타입 지정 방식 +//함수 void, never -//첫 번째 인자는 string, 두 번째 인자는 number -let b: [string, number]; +//void: void는 반환되는 값이 없을 때이다. +function sayHello(): void { + console.log('hello'); +} -b = ['z', 3]; -// b = [3, '3']; //-> error +//never : never는 항상 error를 반환하거나 영원히 끝나지 않는 무한loop일 때 사용한다. +function showError(): never { + throw new Error(); +} -b[0].toLowerCase(); -// b[1].toLowerCase(); //-> error: 숫자에는 대/소문자를 지정할 수 없다. +function infLoop(): never { + while (true) { + //do something... + } +} \ No newline at end of file From a150497698c213c405a16f4e67010b0ecff3e123 Mon Sep 17 00:00:00 2001 From: 4Bee <4bee.code@gmail.com> Date: Tue, 13 Aug 2024 17:28:14 +0900 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=9A=A9:=20=ED=95=A8=EC=88=98=20void?= =?UTF-8?q?=EC=99=80=20never=20=EA=B4=80=EB=A0=A8=20=EC=A3=BC=EC=84=9D=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index cb72a5f..c817b9b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,11 @@ //함수 void, never -//void: void는 반환되는 값이 없을 때이다. +//void: void는 반환되는 값이 없을 때이다. 즉, 함수가 아무런 값을 반환하지 않음을 나타낸다. function sayHello(): void { console.log('hello'); } -//never : never는 항상 error를 반환하거나 영원히 끝나지 않는 무한loop일 때 사용한다. +//never : never는 항상 error를 반환하거나 영원히 끝나지 않는 무한loop일 때 사용한다. 즉, 함수가 절대적으로 반환하지 않는 경우에 사용된다. function showError(): never { throw new Error(); } From 9cce71abe8afe8138b01a13db666db321b79b6eb Mon Sep 17 00:00:00 2001 From: 4Bee <4bee.code@gmail.com> Date: Tue, 13 Aug 2024 17:48:37 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=9A=A9:=20ts=EC=9D=98=20enum=20?= =?UTF-8?q?=EC=84=A0=EC=96=B8=20=EB=B0=A9=EC=8B=9D=EA=B3=BC=20=EB=B0=A9?= =?UTF-8?q?=EB=B2=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index c817b9b..f6feb07 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,17 +1,35 @@ -//함수 void, never +//enum : 특정 값, 공통되는 값을 가지고 있고 싶을 때 enum을 사용하면 된다.enum은 key, value로 두 쌍을 가지고 있다는 특징이 있다. -//void: void는 반환되는 값이 없을 때이다. 즉, 함수가 아무런 값을 반환하지 않음을 나타낸다. -function sayHello(): void { - console.log('hello'); +enum Os { + Window = 3, + Ios = 10, + Android } -//never : never는 항상 error를 반환하거나 영원히 끝나지 않는 무한loop일 때 사용한다. 즉, 함수가 절대적으로 반환하지 않는 경우에 사용된다. -function showError(): never { - throw new Error(); +console.log(Os['Ios']); //10 +console.log(Os[10]); //Ios + +//enum을 숫자가 아닌 문자열로 지정했을 경우 단방향으로만 작용한다. 즉, 키(Key)는 항상 문자열로 정의 해야한다. 값(Value)을 문자열로 정의하면 키와 혼동이 되기 때문에 그렇다는 거지? +enum Os2 { + Window = 'win', + Ios = 'ios', + Android = 'and' } -function infLoop(): never { - while (true) { - //do something... - } -} \ No newline at end of file +//간접 접근 +//key +console.log(Os2['Window']); +console.log(Os2['Ios']); +//value +// console.log(Os2['ios']); //-> error +// console.log(Os2['win']); //-> error key는 항상 문자열로 지정되어 있어서 값(value)가 문자열이면 문제가 발생한다. + +//그렇다면 문자열로 정의 되어 있는 값(value)에 접근을 어떻게 할까? 아래와 같이 접근할 수 있다. + +//직접 접근 +console.log(Os2.Window); +//값 할당을 통한 직접 접근 방식 +let myOs: Os2; + +myOs = Os2.Window +console.log(myOs); From 229a759611063e72bb3a4a89e5b86833f960c54c Mon Sep 17 00:00:00 2001 From: 4Bee <4bee.code@gmail.com> Date: Tue, 13 Aug 2024 17:49:31 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=9A=A9:=20null=EA=B3=BC=20undefined?= =?UTF-8?q?=20=EB=B0=A9=EC=8B=9D=EA=B3=BC=20=EB=B0=A9=EB=B2=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 37 +++---------------------------------- 1 file changed, 3 insertions(+), 34 deletions(-) diff --git a/src/index.ts b/src/index.ts index f6feb07..179268f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,35 +1,4 @@ -//enum : 특정 값, 공통되는 값을 가지고 있고 싶을 때 enum을 사용하면 된다.enum은 key, value로 두 쌍을 가지고 있다는 특징이 있다. +// null, undefined -enum Os { - Window = 3, - Ios = 10, - Android -} - -console.log(Os['Ios']); //10 -console.log(Os[10]); //Ios - -//enum을 숫자가 아닌 문자열로 지정했을 경우 단방향으로만 작용한다. 즉, 키(Key)는 항상 문자열로 정의 해야한다. 값(Value)을 문자열로 정의하면 키와 혼동이 되기 때문에 그렇다는 거지? -enum Os2 { - Window = 'win', - Ios = 'ios', - Android = 'and' -} - -//간접 접근 -//key -console.log(Os2['Window']); -console.log(Os2['Ios']); -//value -// console.log(Os2['ios']); //-> error -// console.log(Os2['win']); //-> error key는 항상 문자열로 지정되어 있어서 값(value)가 문자열이면 문제가 발생한다. - -//그렇다면 문자열로 정의 되어 있는 값(value)에 접근을 어떻게 할까? 아래와 같이 접근할 수 있다. - -//직접 접근 -console.log(Os2.Window); -//값 할당을 통한 직접 접근 방식 -let myOs: Os2; - -myOs = Os2.Window -console.log(myOs); +let a: null = null; +let b: undefined = undefined; \ No newline at end of file