TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์ œ๋„ค๋ฆญ ํด๋ž˜์Šค

Rainbow๐ŸŒˆCoder 2022. 4. 25. 19:03
728x90
//๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊พธ๊ธฐ
//either: a or b
interface Either<T, U> {
  left: () => T;
  right: () => U;
}
class SimpleEither<T, U> implements Either<T, U> {
  constructor(private leftValue: T, private rightValue: U) {}
  left(): T {
    return this.leftValue;
  }
  right(): U {
    return this.rightValue;
  }
}

const either = new SimpleEither(4, { name: "ํ™๊ธธ๋™", age: 18 });
console.log(either.left()); //4
console.log(either.right()); //ํ™๊ธธ๋™โ€‹
//either: a or b
interface Either {
  left: () => number;
  right: () => number;
}
class SimpleEither implements Either {
  constructor(private leftValue: number, private rightValue: number) {}
  left(): number {
    return this.leftValue;
  }
  right(): number {
    return this.rightValue;
  }
}

const either = new SimpleEither(4, 5);
console.log(either.left()); //4
console.log(either.right()); //5

 

๊ฐ™์€ ํƒ€์ž…์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊พธ๊ธฐ

{
  //๊ฐ™์€ ํƒ€์ž…์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊พธ๊ธฐ
  //either: a or b
  interface Either<T> {
    left: () => T;
    right: () => T;
  }
  class SimpleEither<T> implements Either<T> {
    constructor(private leftValue: T, private rightValue: T) {}
    left(): T {
      return this.leftValue;
    }
    right(): T {
      return this.rightValue;
    }
  }

  const either: Either<string> = new SimpleEither("์‹ฌ์ฒญ์ด", "ํ™ฉ์ง„์ด");
  console.log(either.left()); //์‹ฌ์ฒญ์ด
  console.log(either.right()); //ํ™ฉ์ง„์ด
}

๋ช…์‹œ ์•ˆ ํ•ด์ฃผ์–ด๋„ ๋จ

{
  //๊ฐ™์€ ํƒ€์ž…์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊พธ๊ธฐ
  //either: a or b
  interface Either<T> {
    left: () => T;
    right: () => T;
  }
  class SimpleEither<T> implements Either<T> {
    constructor(private leftValue: T, private rightValue: T) {}
    left(): T {
      return this.leftValue;
    }
    right(): T {
      return this.rightValue;
    }
  }

  const either = new SimpleEither("์‹ฌ์ฒญ์ด", "ํ™ฉ์ง„์ด");
  console.log(either.left()); //์‹ฌ์ฒญ์ด
  console.log(either.right()); //ํ™ฉ์ง„์ด
}

 

 

๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊พธ๊ธฐ

{
  //๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊พธ๊ธฐ
  //either: a or b
  interface Either<T, U> {
    left: () => T;
    right: () => U;
  }
  class SimpleEither<T, U> implements Either<T, U> {
    constructor(private leftValue: T, private rightValue: U) {}
    left(): T {
      return this.leftValue;
    }
    right(): U {
      return this.rightValue;
    }
  }

  const either: Either<number, string> = new SimpleEither(4, "ํ™๊ธธ๋™");
  console.log(either.left()); //4
  console.log(either.right()); //ํ™๊ธธ๋™

๋ช…์‹œ ์•ˆ ํ•ด์ฃผ์–ด๋„ ๋จ

  //๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊พธ๊ธฐ
  //either: a or b
  interface Either<T, U> {
    left: () => T;
    right: () => U;
  }
  class SimpleEither<T, U> implements Either<T, U> {
    constructor(private leftValue: T, private rightValue: U) {}
    left(): T {
      return this.leftValue;
    }
    right(): U {
      return this.rightValue;
    }
  }

  const either = new SimpleEither(4, "ํ™๊ธธ๋™");
  console.log(either.left()); //4
  console.log(either.right()); //ํ™๊ธธ๋™
}

์‹ฌ์ง€์–ด ์˜ค๋ธŒ์ ํŠธ ํƒ€์ž…๋„ ๋จ.

์ด์ฒ˜๋Ÿผ ์ œ๋„ค๋ฆญ์„ ์ด์šฉํ•˜๋ฉด ํ™œ์šฉ์„ฑ ๋†’์€ ํ•จ์ˆ˜์™€ ํ™œ์šฉ์„ฑ ๋†’์€ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90