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
'TypeScript > ํ์ ์คํฌ๋ฆฝํธ TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ์ ์คํฌ๋ฆฝํธ] ์ ๋ค๋ฆญ ํ์ ๋ช ์ ์ฃผ์์ฌํญ (0) | 2022.04.26 |
---|---|
[ํ์ ์คํฌ๋ฆฝํธ ๊ณตํ] User-Defined Type Guards (0) | 2022.04.26 |
[ํ์ ์คํฌ๋ฆฝํธ] ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ filter์ map (0) | 2022.04.25 |
[ํ์ ์คํฌ๋ฆฝํธ] ๊ฐ์ฒด์งํฅ์ ์ผ๋ก Generic Stack<T> ํด๋์ค ๊ตฌํํ๊ธฐ (0) | 2022.04.25 |
[ํ์ ์คํฌ๋ฆฝํธ] ์ ๋ค๋ฆญ (0) | 2022.04.25 |