๐Ÿ˜Ž
Onemorebottlee's TIL
  • ๐Ÿค“Introduce
  • ๐Ÿ“–DevLog
    • ๐Ÿ’ฃError
      • npm ERR! Cannot read properties of null (reading 'edgesOut')
      • git
        • src refspec main does not match any
      • NextJS
        • No img element
        • ReferenceError : document is not defined
        • `next/image` Un-configured Host
  • ๐Ÿ“บLecture
    • Nomad Coders
      • ์ดˆ๋ณด์ž๋ฅผ ์œ„ํ•œ ๋ฆฌ๋•์Šค 101
        • #0 Introduction
        • #1 PURE REDUX: COUNTER
        • #2 PURE REDUX: TO DO LIST
        • #3 REACT REDUX
        • #4 Redux Toolkit
      • ๋ฐ”๋‹๋ผ JS๋กœ ๊ทธ๋ฆผ ์•ฑ ๋งŒ๋“ค๊ธฐ
      • React JS ๋งˆ์Šคํ„ฐ ํด๋ž˜์Šค
        • #2 Styled Componnents
        • #3 TypeScript
        • #4 Crypto Tracker
        • #5 State Management
        • #6 Trello
        • #7 ANIMATIONS
      • Next.js ์‹œ์ž‘ํ•˜๊ธฐ
        • #0 INTRODUCTION
        • #1 FRAMEWORK OVERVIEW
        • #2 PRACTICE PROJECT
      • Typescript๋กœ ๋ธ”๋ก์ฒด์ธ ๋งŒ๋“ค๊ธฐ
        • #1 Introduction
        • #2 Overview of TypeScript
        • #3 Functions
        • #4 Classes and Interfaces
        • #5 TypeScript Blockchain
      • SQL Master Class
        • #1 Introduction
        • #2 SQLite
        • #3 Data Definition Language
        • #4 Data Manipulation Language
        • #5 Subqueries and CTEs
        • #6 Indexes
        • #7 MySQL
        • #8 Foreign Keys
        • #9 JOINS
        • #10 Nomalization
        • #11 Events & Triggers
        • #12 Full-Text Indexes
        • #13 PostgreSQL
        • #14 Functions And Procedures
        • #15 Transactions
        • #16 Data Control Language
        • #17 PostgreSQL JSON Columns
        • #18 PostgreSQL Extensions
        • #19 MongoDB
        • #20 REDIS
        • #21 Javascript and Python Drivers
    • Udemy
      • TypeScript ๋งˆ์Šคํ„ฐ with Webpack & React
        • [S1] ์†Œ๊ฐœ
        • [S2] ์„ค์น˜ ๋ฐ ์„ค์ •
        • [S3] ํƒ€์ž… ์• ๋„ˆํ…Œ์ด์…˜ ๊ธฐ์ดˆ
        • [S4] ํ•จ์ˆ˜
        • [S5] ๊ฐ์ฒด ํƒ€์ž…
        • [S6] ๋ฐฐ์—ด ํƒ€์ž…
        • [S7] ์œ ๋‹ˆ์˜จํƒ€์ž…
        • [S8] Tuple & Enum
        • [S9] ์ธํ„ฐํŽ˜์ด์Šค
        • [S10] TypeScript ์ปดํŒŒ์ผ๋Ÿฌ
        • [S11] ๋ฏธ๋‹ˆ ํ”„๋กœ์ ํŠธ DOM, ํƒ€์ž… ๋‹จ์–ธ, ๊ทธ๋ฆฌ๊ณ  ๋” ๋งŽ์€ ๋‚ด์šฉ
        • [S12] Class
        • [S13] TS Class
        • [S14] Generics โญโญโญโญโญ
        • [S15] Narrowing
        • [S16] Type Declarations
        • [S17] Module
        • [S18] Webpack ๊ณผ TypeScript
        • [S19] React
    • ๋ชจ๋‘๋ฅผ ์œ„ํ•œ ์ปดํ“จํ„ฐ ๊ณผํ•™ CS50
      • 1. ์ปดํ“จํŒ… ์‚ฌ๊ณ 
      • 2. C์–ธ์–ด
      • 3. ๋ฐฐ์—ด
      • 4. ์•Œ๊ณ ๋ฆฌ์ฆ˜
      • 5. ๋ฉ”๋ชจ๋ฆฌ
      • 6. ์ž๋ฃŒ๊ตฌ์กฐ
    • ์ƒํ™œ์ฝ”๋”ฉ
      • DATABASE
        • DATABASE1
  • ๐Ÿ“šBook
    • ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive
      • 1์žฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
      • 2์žฅ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ž€?
      • 3์žฅ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ๊ณผ ์‹คํ–‰ ๋ฐฉ๋ฒ•
      • 4์žฅ ๋ณ€์ˆ˜
      • 5์žฅ ํ‘œํ˜„์‹๊ณผ ๋ฌธ
      • 6์žฅ ๋ฐ์ดํ„ฐ ํƒ€์ž…
      • 7์žฅ ์—ฐ์‚ฐ์ž
      • 8์žฅ ์ œ์–ด๋ฌธ
      • 9์žฅ ํƒ€์ž… ๋ณ€ํ™˜๊ณผ ๋‹จ์ถ• ํ‰๊ฐ€ โญโญโญ
      • 10์žฅ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด
      • 11์žฅ ์›์‹œ ๊ฐ’๊ณผ ๊ฐ์ฒด์˜ ๋น„๊ต
      • 12์žฅ ํ•จ์ˆ˜ โญโญโญโญ
      • 13์žฅ ์Šค์ฝ”ํ”„
      • 14์žฅ ์ „์—ญ ๋ณ€์ˆ˜์˜ ๋ฌธ์ œ์ 
      • 15์žฅ let, const ํ‚ค์›Œ๋“œ์™€ ๋ธ”๋ก ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„ โญโญโญ
      • 16์žฅ ํ”„๋กœํผํ‹ฐ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ
      • 17์žฅ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์˜ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ
      • 18์žฅ ํ•จ์ˆ˜์™€ ์ผ๊ธ‰ ๊ฐ์ฒด โญโญ
      • 19์žฅ ํ”„๋กœํ† ํƒ€์ž… โญโญโญโญโญ
      • 20์žฅ strict mode
      • 21์žฅ ๋นŒํŠธ์ธ ๊ฐ์ฒด โญ
      • 22์žฅ this โญโญโญ
      • 23์žฅ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ โญโญโญโญโญโญโญโญโญโญ
      • 24์žฅ ํด๋กœ์ € โญโญโญโญโญโญโญ
      • 25์žฅ ํด๋ž˜์Šค
      • 26์žฅ ES6 ํ•จ์ˆ˜์˜ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ
      • 27์žฅ ๋ฐฐ์—ด โญโญโญ
      • 28์žฅ Number
      • 29์žฅ Math
      • 30์žฅ Date
      • 31์žฅ RegExp
      • 32์žฅ String
      • 33์žฅ 7๋ฒˆ์งธ ๋ฐ์ดํ„ฐ ํƒ€์ž… Symbol
      • 34์žฅ ์ดํ„ฐ๋Ÿฌ๋ธ”
      • 35์žฅ ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•
      • 36์žฅ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น
      • 37์žฅ Set๊ณผ Map
      • 38์žฅ ๋ธŒ๋ผ์šฐ์ €์˜ ๋ Œ๋”๋ง ๊ณผ์ •โญโญโญโญโญโญโญโญ
      • 39์žฅ DOM โญโญโญโญโญโญโญโญโญโญโญ
      • 40์žฅ ์ด๋ฒคํŠธโญโญโญโญโญโญโญโญโญโญโญ
      • 41์žฅ ํƒ€์ด๋จธ
      • 42์žฅ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ โญโญโญโญโญโญโญโญ
      • 43์žฅ Ajax
      • 44์žฅ REST API
      • 45์žฅ ํ”„๋กœ๋ฏธ์Šค
      • 46์žฅ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์™€ async/await
      • 47์žฅ ์—๋Ÿฌ ์ฒ˜๋ฆฌ
      • 48์žฅ ๋ชจ๋“ˆ
      • 49์žฅ Babel๊ณผ Webpack์„ ์ด์šฉํ•œ ES6+/ES.NEXT ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๊ตฌ์ถ•
    • ๋Ÿฌ๋‹ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
      • Ch.1 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ
      • Ch.2 ํƒ€์ž… ์‹œ์Šคํ…œ
      • Ch.3 ์œ ๋‹ˆ์–ธ๊ณผ ๋ฆฌํ„ฐ๋Ÿด
      • Ch.4 ๊ฐ์ฒด
      • Ch.5 ํ•จ์ˆ˜
      • Ch.6 ๋ฐฐ์—ด
      • Ch.7 ์ธํ„ฐํŽ˜์ด์Šค
      • Ch.8 ํด๋ž˜์Šค
      • Ch.9 ํƒ€์ž… ์ œํ•œ์ž
      • Ch.10 ์ œ๋„ค๋ฆญ
      • Ch.11 ์„ ์–ธ ํŒŒ์ผ
      • Ch.12 IDE ๊ธฐ๋Šฅ ์‚ฌ์šฉ
      • Ch.13 ๊ตฌ์„ฑ ์˜ต์…˜
      • Ch.14 ๊ตฌ๋ฌธ ํ™•์žฅ
      • Ch.15 ํƒ€์ž… ์šด์˜
      • ์šฉ์–ด ์‚ฌ์ „
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์™„๋ฒฝ ๊ฐ€์ด๋“œ
    • SQL in 10 Minutes
      • 1์žฅ SQL ์ดํ•ดํ•˜๊ธฐ
      • 2์žฅ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
      • 3์žฅ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ ์ •๋ ฌํ•˜๊ธฐ
      • 4์žฅ ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง
      • 5์žฅ ๊ณ ๊ธ‰ ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง
      • 6์žฅ ์™€์ผ๋“œ์นด๋“œ ๋ฌธ์ž๋ฅผ ์ด์šฉํ•œ ํ•„ํ„ฐ๋ง
      • 7์žฅ ๊ณ„์‚ฐ ํ•„๋“œ ์ƒ์„ฑํ•˜๊ธฐ
      • 8์žฅ ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ
      • 9์žฅ ๋ฐ์ดํ„ฐ ์š”์•ฝ
      • 10์žฅ ๋ฐ์ดํ„ฐ ๊ทธ๋ฃนํ•‘
      • 11์žฅ ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ
      • 12์žฅ ํ…Œ์ด๋ธ” ์กฐ์ธ
      • 13์žฅ ๊ณ ๊ธ‰ ํ…Œ์ด๋ธ” ์กฐ์ธ ์ƒ์„ฑํ•˜๊ธฐ
      • 14์žฅ ์ฟผ๋ฆฌ ๊ฒฐํ•ฉํ•˜๊ธฐ
      • 15์žฅ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…ํ•˜๊ธฐ
      • 16์žฅ ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ์™€ ์‚ญ์ œ
      • 17์žฅ ํ…Œ์ด๋ธ” ์ƒ์„ฑ๊ณผ ์กฐ์ž‘
      • 18์žฅ ๋ทฐ ์‚ฌ์šฉํ•˜๊ธฐ
      • 19์žฅ ์ €์žฅ ํ”„๋กœ์‹œ์ € ์‚ฌ์šฉํ•˜๊ธฐ
      • 20์žฅ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ ๊ด€๋ฆฌํ•˜๊ธฐ
      • 21์žฅ ์ปค์„œ ์‚ฌ์šฉํ•˜๊ธฐ
      • 22์žฅ ๊ณ ๊ธ‰ ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ์˜ต์…˜
    • ๋ฉด์ ‘์„ ์œ„ํ•œ CS ์ „๊ณต์ง€์‹ ๋…ธํŠธ
      • 4 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
        • 4.1 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ธฐ๋ณธ
    • 2023 ์ด๊ธฐ์  SQL ๊ฐœ๋ฐœ์ž ์ด๋ก ์„œ + ๊ธฐ์ถœ๋ฌธ์ œ
      • ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ์ดํ•ด
        • S1. ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง
  • ๐Ÿ’ปStudy
    • CS ์ง€์‹ ๋ฐœํ‘œ
      • 1ํšŒ์ฐจ
      • 2ํšŒ์ฐจ
      • 3ํšŒ์ฐจ
      • 4ํšŒ์ฐจ
      • 5ํšŒ์ฐจ
      • 6ํšŒ์ฐจ
      • 7ํšŒ์ฐจ
      • 8ํšŒ์ฐจ
      • 9ํšŒ์ฐจ
    • TypeScript Exercises
      • Ex 1
      • Ex 2
      • Ex 3
      • Ex 4
      • Ex 5
      • Ex 6
      • Ex 7
      • Ex 8
      • Ex 9
      • Ex 10
  • ๐Ÿ”„ETC
    • Article
      • Atomic Design Pattern
      • ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž
    • DATABASE
      • Oracle
        • CEIL() & FLOOR() - ์†Œ์ˆ˜์  ์˜ฌ๋ฆผ & ๋‚ด๋ฆผ
        • DUAL ํ…Œ์ด๋ธ” - ์ž„์‹œ ํ…Œ์ด๋ธ”๋กœ ๊ฒฐ๊ณผ ํ™•์ธํ•˜๊ธฐ
    • Ubuntu
      • ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์—…๋ฐ์ดํŠธ
Powered by GitBook
On this page
  • ๋ฌธ์ œ
  • ํ’€์ด

Was this helpful?

  1. Study
  2. TypeScript Exercises

Ex 7

๋ฌธ์ œ

/*

Intro:

    Filtering was completely removed from the project.
    It turned out that this feature was just not needed
    for the end-user and we spent a lot of time just because
    our office manager told us to do so. Next time we should
    instead listen to the product management.

    Anyway we have a new plan. CEO's friend Nick told us
    that if we randomly swap user names from time to time
    in the community, it would be very funny and the project
    would definitely succeed!

Exercise:

    Implement swap which receives 2 persons and returns them in
    the reverse order. The function itself is already
    there, actually. We just need to provide it with proper types.
    Also this function shouldn't necessarily be limited to just
    Person types, lets type it so that it works with any two types
    specified.

*/

interface User {
    type: 'user';
    name: string;
    age: number;
    occupation: string;
}

interface Admin {
    type: 'admin';
    name: string;
    age: number;
    role: string;
}

function logUser(user: User) {
    const pos = users.indexOf(user) + 1;
    console.log(` - #${pos} User: ${user.name}, ${user.age}, ${user.occupation}`);
}

function logAdmin(admin: Admin) {
    const pos = admins.indexOf(admin) + 1;
    console.log(` - #${pos} Admin: ${admin.name}, ${admin.age}, ${admin.role}`);
}

const admins: Admin[] = [
    {
        type: 'admin',
        name: 'Will Bruces',
        age: 30,
        role: 'Overseer'
    },
    {
        type: 'admin',
        name: 'Steve',
        age: 40,
        role: 'Steve'
    }
];

const users: User[] = [
    {
        type: 'user',
        name: 'Moses',
        age: 70,
        occupation: 'Desert guide'
    },
    {
        type: 'user',
        name: 'Superman',
        age: 28,
        occupation: 'Ordinary person'
    }
];

export function swap(v1, v2) {
    return [v2, v1];
}

function test1() {
    console.log('test1:');
    const [secondUser, firstAdmin] = swap(admins[0], users[1]);
    logUser(secondUser);
    logAdmin(firstAdmin);
}

function test2() {
    console.log('test2:');
    const [secondAdmin, firstUser] = swap(users[0], admins[1]);
    logAdmin(secondAdmin);
    logUser(firstUser);
}

function test3() {
    console.log('test3:');
    const [secondUser, firstUser] = swap(users[0], users[1]);
    logUser(secondUser);
    logUser(firstUser);
}

function test4() {
    console.log('test4:');
    const [firstAdmin, secondAdmin] = swap(admins[1], admins[0]);
    logAdmin(firstAdmin);
    logAdmin(secondAdmin);
}

function test5() {
    console.log('test5:');
    const [stringValue, numericValue] = swap(123, 'Hello World');
    console.log(` - String: ${stringValue}`);
    console.log(` - Numeric: ${numericValue}`);
}

[test1, test2, test3, test4, test5].forEach((test) => test());

// In case you are stuck:
// https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types
// https://www.typescriptlang.org/docs/handbook/2/generics.html

ํ’€์ด

swap ํ•จ์ˆ˜๋Š” ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ” ๋ฐฐ์—ด์— ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ œ๋„ค๋ฆญ์„ ํ™œ์šฉํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด๊ฒฐํ•œ๋‹ค.

export function swap<T, V>(v1:T, v2:V): [V,T] {
    return [v2, v1];
}

PreviousEx 6NextEx 8

Last updated 1 year ago

Was this helpful?

๐Ÿ’ป