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