Dream๐Ÿฐng
article thumbnail
[TS] Drag & Drop 2๏ธโƒฃ
๐Ÿ“ Study/TypeScript 2023. 3. 30. 01:33

๐Ÿ‘ Drag & Drop 2๏ธโƒฃ Tuple is an array tuple์€ JavaScript์—๋Š” ์—†๊ณ  TypeScript์—๋งŒ ์žˆ๋Š” ํƒ€์ž…์ด๋‹ค. ๋”ฐ๋ผ์„œ ํŠน์ • ๋ณ€์ˆ˜๋ฅผ tuple ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ–ˆ์„ ๋•Œ, ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ tuple ํƒ€์ž…์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์—†๋‹ค. const engineer: [string, string, string] = ['Apeach', 'Ogu', 'Choonsik']; engineer instanceof Tuple; // โŒ typeof engineer === 'tuple'; // โŒ (false) But tuple์€ TypeScript์—๋งŒ ์กด์žฌํ•˜๋Š” ํƒ€์ž…์ด์ง€๋งŒ JavaScript๋กœ ์ƒ๊ฐํ•ด๋ดค์„ ๋•Œ ๊ฒฐ๊ตญ์€ ๋ฐฐ์—ด์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๊ฐ€ tuple์ด ๋งž๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด ๋ฐฐ์—ด์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธ..

article thumbnail
[TS] Project Drag & Drop 1๏ธโƒฃ
๐Ÿ“ Study/TypeScript 2023. 3. 27. 12:31

Project Drag & Drop 1๏ธโƒฃ ! & as TypeScript์—์„œ DOM ์กฐ์ž‘ํ•  ๋•Œ ์˜ค๋ฅ˜ ํ•ด๊ฒฐํ•˜๊ธฐ class ProjectInput { templateElement: HTMLTemplateElement; hostElement: HTMLDivElement; constructor() { this.templateElement = document.getElementById( 'project-input' )! as HTMLTemplateElement; this.hostElement = document.getElementById('app')! as HTMLDivElement; } } document.getElementById('id');์—์„œ !๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ด์œ ๋Š” TypeScript๊ฐ€ ์ด๊ฒƒ์ด null์ผ์ˆ˜..

article thumbnail
[TS] Decorators 3๏ธโƒฃ
๐Ÿ“ Study/TypeScript 2023. 3. 24. 19:36

Decorators 3๏ธโƒฃ Validation decorator ํ”„๋กœํผํ‹ฐ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์˜ˆ์‹œ class Course { title: string; price: number; constructor(t: string, p: number) { this.title = t; this.price = p; } } const courseForm = document.querySelector('form')!; courseForm.addEventListener('submit', event => { event.preventDefault(); const titleEl = document.getElementById('title') as HTMLInputElement; const priceEl = document.getElementB..

article thumbnail
[TS] Decorators 2๏ธโƒฃ
๐Ÿ“ Study/TypeScript 2023. 3. 22. 11:56

Decorators 2๏ธโƒฃ Decorator the synthetic sugar ํด๋ž˜์Šค ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” constructor๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด์„œ ๊ธฐ์กด constructor๋ฅผ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. constructor๋ฅผ ํ™•์žฅํ•  ๋•Œ super();๋Š” ๊ธฐ์กด ํด๋ž˜์Šค ํ™•์žฅ์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ธฐ์กด ํด๋ž˜์Šค์˜ ์ •๋ณด๋ฅผ ๊ธฐ์–ตํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. function ClassDeco() { return function(target: T) { return class extends target { constructor(..._: any[]) { super(); // not necessary /* write your code */ } } } } @ClassDeco class Person { name = 'Shou'; get name() {..

article thumbnail
[TS] Decorators 1๏ธโƒฃ
๐Ÿ“ Study/TypeScript 2023. 3. 20. 12:51

Decorators 1๏ธโƒฃ What is Decorators? ํด๋ž˜์Šค, ํ”„๋กœํผํ‹ฐ, ์•ก์„ธ์„œ, ๋ฉ”์†Œ๋“œ, ํŒŒ๋ผ๋ฏธํ„ฐ์— ์ฒจ๋ถ€ํ•  ์ˆ˜ ์žˆ๋Š” ํŠน๋ณ„ํ•œ ํ•จ์ˆ˜์ด๋‹ค. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๊ฐ€ ๋ถ™์€ ํด๋ž˜์Šค ๋“ฑ๋“ฑ ์—์„œ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์—์„œ ์ •์˜๋œ ๊ธฐ๋Šฅ์ด ๋™์ž‘ํ•œ๋‹ค. ์ •์˜ํ•œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋ฅผ @ ๊ธฐํ˜ธ๋ฅผ ๋ถ™์—ฌ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. function ClassDeco(constructor: Function) { console.log('Logging...'); console.log(constructor); } @ClassDeco class Person { name = 'Shou'; constructor() { console.log('Creating person object...'); } } /* Logging... class Person { constructo..

article thumbnail
[TS] Generics 2๏ธโƒฃ
๐Ÿ“ Study/TypeScript 2023. 3. 17. 12:07

Generics 2๏ธโƒฃ Keyof Constraint ์ œ๋„ค๋ฆญ์œผ๋กœ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ œ์•ฝ ์กฐ๊ฑด์„ ์ค„ ๋•Œ, ํ‚ค ๊ฐ’์„ ํ•„์š”๋กœ ํ•œ๋‹ค๋ฉด keyof๋ฅผ ์‚ฌ์šฉํ•œ ์ œ์•ฝ ์กฐ๊ฑด์„ ์“ฐ๋ฉด ์‹ค์ˆ˜๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. function extractAndConvert( obj: T, key: U ) { return 'Value: ' + obj[key]; } extractAndConvert({}, 'name'); // Error occurred ์ œ๋„ค๋ฆญ T์˜ ์ œ์•ฝ ์กฐ๊ฑด์œผ๋กœ object๋ฅผ ์„ค์ •ํ•ด์ฃผ์—ˆ๊ณ , U์˜ ์ œ์•ฝ ์กฐ๊ฑด์œผ๋กœ T์˜ key๋ฅผ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค. extractAndConvert๋กœ ๋นˆ ๊ฐ์ฒด({})์™€ name์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ „๋‹ฌํ–ˆ์„ ๋•Œ, ๋นˆ ๊ฐ์ฒด์—๋Š” name์ด๋ผ๋Š” ํ‚ค ๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค! ์ œ๋„ค๋ฆญ์œผ๋กœ U extends keyof..

article thumbnail
[TS] Generics 1๏ธโƒฃ
๐Ÿ“ Study/TypeScript 2023. 3. 16. 18:51

Generics 1๏ธโƒฃ What is Generics? ์ œ๋„ค๋ฆญ์€ vanilla JavaScript์—๋Š” ์—†๊ณ  TypeScript์—๋งŒ ์žˆ๋Š” ๊ฐœ๋…์œผ๋กœ, ํŠน์ • ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์—ฌ ํƒ€์ž… ์•ˆ์ „์„ฑ์„ ํ™•๋ณดํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ฒ•์ด๋‹ค. TypeScript์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์ œ๋„ค๋ฆญ ํƒ€์ž…์œผ๋กœ๋Š” Array, Promise๊ฐ€ ์žˆ๋‹ค. // Array const names: Array = []; /* Array์— ๋Œ€ํ•œ ํƒ€์ž…์„ string์œผ๋กœ ์ง€์ •ํ•˜๋ฉด(= Generic) ์•ž์œผ๋กœ names ๋ฐฐ์—ด์— ๋“ค์–ด์˜ฌ ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์ด string์ž„์„ ์ถ”๋ก ํ•จ */ // Promise const promise: Promise = new Promise((resolve, reject) => { setTimeout(() => { resolve(10); ..

profile on loading

Loading...