From 126b05951b339de77e668cfb0330d0a465389365 Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Thu, 26 Sep 2024 01:30:50 +0900 Subject: [PATCH 01/14] =?UTF-8?q?=F0=9F=9A=A9:=20number=20=ED=83=80?= =?UTF-8?q?=EC=9E=85=20=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9207da2..a3b5c90 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,10 @@ // src/index.ts -const message: string = "Hello, TypeScript!"; -console.log(message); + +function add(num1:number, num2:number){ // 타입 정의 + console.log(num1 + num2); +} + +add(1,2); + +// 다른 타입 사용시 에러 발생 +// add("hello", "world"); From 1b9597c60aa8d38bfa2a1d61152847efa9a9f705 Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Thu, 26 Sep 2024 01:35:21 +0900 Subject: [PATCH 02/14] =?UTF-8?q?=F0=9F=9A=A9:=20=EC=88=AB=EC=9E=90?= =?UTF-8?q?=ED=98=95=20=EB=B0=B0=EC=97=B4=20=ED=8C=8C=EB=9D=BC=EB=AF=B8?= =?UTF-8?q?=ED=84=B0=20=ED=95=A8=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/index.ts b/src/index.ts index a3b5c90..039f278 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ // src/index.ts +// number 파라미터를 받는 함수 function add(num1:number, num2:number){ // 타입 정의 console.log(num1 + num2); } @@ -8,3 +9,13 @@ add(1,2); // 다른 타입 사용시 에러 발생 // add("hello", "world"); + + +// 숫자형 배열 파라미터 함수 +function showItems(arr: number[]){ + arr.forEach((item)=>{ + console.log(item); + }); +} + +showItems([1, 2, 3]); From d759f05d8d9d0672c08eb770fbc0d16cfe63c13f Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Thu, 26 Sep 2024 11:25:41 +0900 Subject: [PATCH 03/14] =?UTF-8?q?=F0=9F=9A=A9:=20PR=20=ED=85=9C=ED=94=8C?= =?UTF-8?q?=EB=A6=BF=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/PULL_REQUEST_TEMPLATE.md | 9 +++++++++ src/index.ts | 22 ++-------------------- 2 files changed, 11 insertions(+), 20 deletions(-) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..9b61b85 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,9 @@ +✍ // lecture/num : task + + +📝Description +// Description 작성 + + +📌Summary +// Summary 작성 \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 039f278..9207da2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,21 +1,3 @@ // src/index.ts - -// number 파라미터를 받는 함수 -function add(num1:number, num2:number){ // 타입 정의 - console.log(num1 + num2); -} - -add(1,2); - -// 다른 타입 사용시 에러 발생 -// add("hello", "world"); - - -// 숫자형 배열 파라미터 함수 -function showItems(arr: number[]){ - arr.forEach((item)=>{ - console.log(item); - }); -} - -showItems([1, 2, 3]); +const message: string = "Hello, TypeScript!"; +console.log(message); From ea16d6ac94a2c743e4523ce35d78d5bc47a392ea Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Thu, 26 Sep 2024 15:38:43 +0900 Subject: [PATCH 04/14] =?UTF-8?q?=F0=9F=9A=A9:=20PR=20=ED=85=9C=ED=94=8C?= =?UTF-8?q?=EB=A6=BF=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/PULL_REQUEST_TEMPLATE.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 9b61b85..e3b6937 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,9 +1,7 @@ -✍ // lecture/num : task +### ✍ // lecture/num : task - -📝Description +### 📝Description // Description 작성 - -📌Summary +### 📌Summary // Summary 작성 \ No newline at end of file From 93e462bc69e97b24357cc90e796c511fc946a1d4 Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Wed, 2 Oct 2024 20:07:11 +0900 Subject: [PATCH 05/14] =?UTF-8?q?=F0=9F=9A=A9:=20keyof=20=ED=82=A4?= =?UTF-8?q?=EC=9B=8C=EB=93=9C=20=EC=82=AC=EC=9A=A9=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9207da2..03fcffd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,14 @@ -// src/index.ts -const message: string = "Hello, TypeScript!"; -console.log(message); +// keyof + +interface User{ + id: number; + name: string; + age: number; + gender: "m" | "f"; +} + +type USerKey = keyof User; +// keyof 키워드를 사용해서 키값들을 union 형태로 받을 수 있다. +// 'id' | 'name' | 'age' | 'gender' + +const uk:USerKey = "id"; From da214aa6b08d2c64bc1d9e7a01f9378dc7558ecf Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Wed, 2 Oct 2024 20:09:27 +0900 Subject: [PATCH 06/14] =?UTF-8?q?=F0=9F=9A=A9:=20Partial=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 03fcffd..3314e12 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,15 @@ -// keyof +// Partial +// 프로퍼티를 모두 optional로 변경해준다. +// 일부만 사용하는 것이 가능해진다. -interface User{ +interface User { id: number; name: string; age: number; gender: "m" | "f"; } -type USerKey = keyof User; -// keyof 키워드를 사용해서 키값들을 union 형태로 받을 수 있다. -// 'id' | 'name' | 'age' | 'gender' - -const uk:USerKey = "id"; +let admin: Partial = { + id: 1, + name: "Bob", +} From df307dd92e736b86a24e2c9501c7dd67b31b9216 Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Wed, 2 Oct 2024 20:11:34 +0900 Subject: [PATCH 07/14] =?UTF-8?q?=F0=9F=9A=A9:=20Requird?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3314e12..32308d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,14 @@ -// Partial -// 프로퍼티를 모두 optional로 변경해준다. -// 일부만 사용하는 것이 가능해진다. +// Required +// 모든 프로퍼티를 필수로 변경해준다. -interface User { +interface User{ id: number; name: string; - age: number; - gender: "m" | "f"; + age?: number; } -let admin: Partial = { +let admin: Required = { id: 1, name: "Bob", -} + age: 30 +} \ No newline at end of file From adc59f44f451443c75205d93a05061fcee858900 Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Wed, 2 Oct 2024 20:13:40 +0900 Subject: [PATCH 08/14] =?UTF-8?q?=F0=9F=9A=A9:=20Readonly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 32308d7..bc93c38 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -// Required -// 모든 프로퍼티를 필수로 변경해준다. +// Readonly +// 모든 프로퍼티를 읽기전용으로 변경 interface User{ id: number; @@ -7,8 +7,9 @@ interface User{ age?: number; } -let admin: Required = { +let admin: Readonly = { id: 1, name: "Bob", - age: 30 -} \ No newline at end of file +} + +// admin.id = 4; // 에러 발생. 할당만 가능하고 수정 불가. \ No newline at end of file From ec65b15aa2a95096584aabf120acb5a05026f918 Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Wed, 2 Oct 2024 20:20:08 +0900 Subject: [PATCH 09/14] =?UTF-8?q?=F0=9F=9A=A9:=20Record?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index bc93c38..aaa47a0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,23 @@ -// Readonly -// 모든 프로퍼티를 읽기전용으로 변경 +// Record // K=key, T=type + +const score: Record<'1'|'2'|'3'|'4', "A"|"B"|"C"|"D"> = { + 1: "A", + 2: "C", + 3: "B", + 4: "D", +} interface User{ id: number; name: string; - age?: number; -} - -let admin: Readonly = { - id: 1, - name: "Bob", + age: number; } -// admin.id = 4; // 에러 발생. 할당만 가능하고 수정 불가. \ No newline at end of file +function isValid(user:User){ + const result: Record ={ + id: user.id>0, + name: user.name !=='', + age: user.age > 0, + }; + return result; +} \ No newline at end of file From e51b5ad2d2e9fa633713a4209ff181ca8467c511 Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Wed, 2 Oct 2024 22:35:05 +0900 Subject: [PATCH 10/14] =?UTF-8?q?=F0=9F=9A=A9:=20Record?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 26 ++++++++++++++++++++++++++ tsconfig.json | 12 +++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/index.js diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..3ab3929 --- /dev/null +++ b/src/index.js @@ -0,0 +1,26 @@ +function merge(objA, objB) { + return Object.assign({}, objA, objB); +} +var mergeObj = merge({ name: 'Max', hobbies: ['Sports'] }, { age: 30 }); +// const mergeObj2 = merge({ name: 'Max' }, { age: 30 }); +console.log(mergeObj.name); +function countAndDescribe(element) { + var descriptionText = "Got no Value"; + if (element.length === 1) { + descriptionText = "Got 1 element"; + } + else if (element.length > 1) { + descriptionText = 'Got ' + element.length + ' elements.'; + } + return [element, descriptionText]; + // return ['Max', 'Tom']; //error 발생 : 타입을 명시했기 때문에 그렇다. +} +console.log(countAndDescribe('Hi, there!')); +//객체 key, value에 합당한 ts Generics +function extractAndConvert(obj, key) { + return 'Value: ' + obj[key]; +} +// T : { name: 'Max' }, U : 'name' +var value = extractAndConvert({ name: 'Max' }, 'name'); +console.log(value); +// 위의 설명이 다소 복잡하다. 우선 U는 객체의 key, value 형태로 구성되어 있다. 그리고 U는 T에서 key에 해당되는 값을 상속받아 key타입을 지니고 있다. 즉, name 부분을 반환하는 형태이다. return 값의 반환을 보면 object의 해당 key값을 도출하려는 의도이다. 따라서, console.log(value)의 값은 Max가 된다. diff --git a/tsconfig.json b/tsconfig.json index d71281c..b153307 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,16 @@ { "compilerOptions": { - "target": "es6", // 컴파일 시 사용할 ECMAScript 버전 + "target": "es6", // 컴파일 시 사용할 ECMAScript 버전 + "lib" : [ + "dom", + "dom.iterable", + "esnext", + ], + "allowJs": true, + "skipLibCheck": true, + "allowSyntheticDefaultImports": true, + "forceConsistentCasingInFileNames": true, + "noFallthroughCasesInSwitch": true, "module": "commonjs", // 모듈 시스템 "strict": true, // 엄격한 타입 검사 "esModuleInterop": true, // ES 모듈과의 상호 운용성 설정 From c9506363354824e9766b1764108ac1c23c9e7c76 Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Wed, 2 Oct 2024 22:38:00 +0900 Subject: [PATCH 11/14] =?UTF-8?q?=F0=9F=9A=A9:=20Pick?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index aaa47a0..376a33b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,23 +1,15 @@ -// Record // K=key, T=type +// Pick -const score: Record<'1'|'2'|'3'|'4', "A"|"B"|"C"|"D"> = { - 1: "A", - 2: "C", - 3: "B", - 4: "D", -} +// T 타입에서 K 프로퍼티만 골라서 사용 interface User{ id: number; name: string; age: number; + gender: "M"|"W"; } -function isValid(user:User){ - const result: Record ={ - id: user.id>0, - name: user.name !=='', - age: user.age > 0, - }; - return result; +const admin: Pick = { + id: 0, + name: "Bob", } \ No newline at end of file From c93266adbf049c1f9801a68a64df7ea9e946ba6a Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Wed, 2 Oct 2024 22:38:50 +0900 Subject: [PATCH 12/14] =?UTF-8?q?=F0=9F=9A=A9:=20Omit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 376a33b..74b7e12 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ -// Pick +// Omit -// T 타입에서 K 프로퍼티만 골라서 사용 +// T 타입에서 K 프로퍼티만 생략해서 사용 interface User{ id: number; @@ -9,7 +9,7 @@ interface User{ gender: "M"|"W"; } -const admin: Pick = { +const admin: Omit = { id: 0, name: "Bob", } \ No newline at end of file From d11c766e758d67cc0e658e41955769ee14b7f92b Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Wed, 2 Oct 2024 22:42:54 +0900 Subject: [PATCH 13/14] =?UTF-8?q?=F0=9F=9A=A9:=20Exclude?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index 74b7e12..aa19a60 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,7 @@ -// Omit +// Exclude -// T 타입에서 K 프로퍼티만 생략해서 사용 +// T1에서 T2를 제외하고 사용 +// Omit은 프로퍼티를 제거, Exclude는 타입을 제거 -interface User{ - id: number; - name: string; - age: number; - gender: "M"|"W"; -} - -const admin: Omit = { - id: 0, - name: "Bob", -} \ No newline at end of file +type T1 = string|number; +type T2 = Exclude; \ No newline at end of file From 1fab994c4da6ffe532ec2975e3d9f0558ef8c687 Mon Sep 17 00:00:00 2001 From: "[nyun-nye]" <[2001_11_7@naver.com]> Date: Wed, 2 Oct 2024 22:48:21 +0900 Subject: [PATCH 14/14] =?UTF-8?q?=F0=9F=9A=A9:=20NonNullable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index aa19a60..708bf26 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,5 @@ -// Exclude +// NonNullable +// Null과 undefind를 제외한 타입 생성 -// T1에서 T2를 제외하고 사용 -// Omit은 프로퍼티를 제거, Exclude는 타입을 제거 - -type T1 = string|number; -type T2 = Exclude; \ No newline at end of file +type T1 = string | null | undefined | void; +type T2 = NonNullable; \ No newline at end of file