[TypeScript] ๐Ÿณ 2.2 ์ปค์Šคํ…€ ํƒ€์ž… ์ •์˜

์กฐ์ค€ํ˜•ยท2021๋…„ 11์›” 3์ผ
0

TypeScript

๋ชฉ๋ก ๋ณด๊ธฐ
7/11
post-thumbnail

๐Ÿณ 2.2 ์ปค์Šคํ…€ ํƒ€์ž… ์ •์˜

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” type, interface, enum ํ‚ค์›Œ๋“œ ๋ฐ ํด๋ž˜์Šค ์„ ์–ธ์œผ๋กœ ์ปค์Šคํ…€ ํƒ€์ž…์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๐ŸŒ 2.2.1 ํƒ€์ž… ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ

์ƒˆ๋กœ์šด ํƒ€์ž…์„ ์„ ์–ธํ•˜๊ฑฐ๋‚˜ ํƒ€์ž… ๋ณ„์นญ์„ ์‚ฌ์šฉํ•ด ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํƒ€์ž…์— ๋‹ค๋ฅธ ์ด๋ฆ„์„ ๋ถ™์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.

// 2_10.ts
type Foot = number;
type Pound = number;
// 2_11.ts
type Patient = {
    name: string;
    height: Foot; // Footํƒ€์ž… ๋ณ„์นญ ์‚ฌ์šฉ
    weight: Pound ; // Poundํƒ€์ž… ๋ณ„์นญ ์‚ฌ์šฉ
}

ํƒ€์ž…๋ณ„์นญ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋กœ ์ปดํŒŒ์ผ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

// 2_12.ts
let patient: Patient = { // ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด ์ธ์Šคํ„ด์Šค ๋งŒ๋“ฌ.
    name: 'Joe Smith',
    height: 5,
    weight: 100,
}

๋งŒ์•ฝ ์—ฌ๊ธฐ์„œ weight๊ฐ€ ๋น ์ง€๋ฉด, weight๊ฐ€ ์—†๋‹ค๊ณ  ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ.

ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ๊ฐ€ ํ•„์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ์˜ต์…˜์ด๋ผ๋ฉด, ํ”„๋กœํผํ‹ฐ ์ด๋ฆ„์— ?๋ฅผ ๋ถ™์—ฌ ์กฐ๊ฑด๋ถ€ ํ”„๋กœํผ ํƒ€์ž…์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

// 2_13.ts
type Patient2_13 = {
    name: string;
    height: Foot; // Footํƒ€์ž… ๋ณ„์นญ ์‚ฌ์šฉ
    weight?: Pound ; // Poundํƒ€์ž… ๋ณ„์นญ ์‚ฌ์šฉ
}

let patient2_13: Patient2_13 = { // ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด ์ธ์Šคํ„ด์Šค ๋งŒ๋“ฌ.
    name: 'Joe Smith',
    height: 5,
}

โ— ํด๋ž˜์Šค๋‚˜ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ๋ฌผ์Œํ‘œ๋ฅผ ์ถ”๊ฐ€ํ•ด ์กฐ๊ฑด๋ถ€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •์˜ ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•จ์ˆ˜ ์‹œ๊ทธ๋‹ˆ์ฒ˜์—๋„ ํƒ€์ž… ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

ํผ ์–‘์‹์ด ์žˆ๊ณ  ์ž…๋ ฅ๋œ ๊ฐ’์˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•˜๋Š” ValidatorFn์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์ž.
ValidatorFnํ•จ์ˆ˜์— ํŠน์ • ์‹œ๊ทธ๋‹ˆ์ฒ˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. FormControlํƒ€์ž… ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„ ๊ฐ’์ด ์œ ํšจํ•œ ๊ฒฝ์šฐ null์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์•„๋‹ˆ๋ฉด ์˜ค๋ฅ˜๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

type ValidatorFn = 
	(c: FormControl) => {[key: string]: any} | null

{[key: string]: any}๋Š” ๋ชจ๋“  ํƒ€์ž…์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

class FormControl{
    constructor (initialValue: string, validator: ValidatorFn | null){...};
}

FormControl ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค ๋•Œ ์•ž์„œ ์ •์˜ํ•œ ์ปค์Šคํ…€ํƒ€์ž… ValidatorFn์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ

๐ŸŒ 2.2.2 ํด๋ž˜์Šค ๋‚ด ์ปค์Šคํ…€ ํƒ€์ž… ์‚ฌ์šฉ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ํด๋ž˜์Šค ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ตฌ๋ฌธ์ด ์—†์ง€๋งŒ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์•„๋‹ˆ๋‹ค.

class Person{
    firstName: string;
    lastname: string;
    age: number;
}

const p = new Person();
p.firstName = 'John';
p.lastName = 'Smith';
p.age = '25';

ts->js ES6๋กœ ๋ณ€ํ™˜

"use strict";
class Person{}

const p = new Person();
p.firstName = 'John';
p.lastName = 'Smith';
p.age = '25';

JS์ฝ”๋“œ์—์„œ Person์ด ๋น„์–ด์žˆ๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Person์ƒ์„ฑ์ž๋ฅผ ์„ ์–ธํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“  ํ›„์— ํ”„๋กœํผํ‹ฐ๋ฅผ ์ดˆ๊ธฐํ™”ํ•จ.
์ƒ์„ฑ์ž๋Š” ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋œ ํ›„์— ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋˜๋Š” ํŠน๋ณ„ํ•œ ํ•จ์ˆ˜์ด๋‹ค.

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ์ฝ”๋“œ ํ•œ์ค„๋กœ ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ•˜๊ณ  ํ”„๋กœํผํ‹ฐ๋ฅผ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ๋‹ค.
์ƒ์„ฑ์ž์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๋“ฑ์— ํƒ€์ž…์„ ํ‘œ๊ธฐ ๊ฐ€๋Šฅ

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋„ ์ ‘๊ทผ์ œ์–ด์ž๊ฐ€ ์žˆ๊ณ  readonly , private, protected, public 4๊ฐœ์˜ ํ‚ค์›Œ๋“œ๊ฐ€ ์žˆ๋‹ค.

class Person{
    constructor(public firstName: string,
                public lastName: string, public age: number) {};
}

const p = new Person('John', 'Smith', 25);

๋ณ€ํ™˜

"use strict";
class Person{
    constructor(firstName, lastName, age){
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
    ;
}
const p = new Person('John', 'Smith', 25);

const p์—์„œ ๋ณ„๋„์˜ ํƒ€์ž… ์—†์ด ์„ ์–ธ ํ–ˆ์ง€๋งŒ

const p:Person = new Person('John', 'Smith', 25);

์œ„์ฒ˜๋Ÿผ ์ˆ˜์ •ํ•˜์—ฌ ๋ช…ํ™•ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

โ— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํด๋ž˜์Šค ๋‚ด ๊ฐ ์ƒ์„ฑ์ž ํŒŒ๋ผ๋ฏธํ„ฐ์— public ์ ‘๊ทผ ์ œ์–ด์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
์ƒ์„ฑ๋œ ํ”„๋กœํผํ‹ฐ๋Š” ํด๋ž˜์Šค ๋‚ด๋ถ€์™€ ์™ธ๋ถ€ ๋ชจ๋‘ ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํด๋ž˜์Šค ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ ์–ธ ํ•  ๋•Œ readonly ์ œํ•œ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
( ํด๋ž˜์Šค ์ƒ์„ฑ์ž ๋‚ด๋ถ€ ๋“ฑ์— ํ”„๋กœํผํ‹ฐ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒฝ์šฐ, ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ )
const์™€ ๋น„์Šทํ•˜์ง€๋งŒ ํด๋ž˜์Šค ํ”„๋กœํผํ‹ฐ์— const๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†๋‹ค.

๐ŸŒ 2.2.3 ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ ์ปค์Šคํ…€ ํƒ€์ž…

์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ฐ์ฒดํ”„๋กœํผํ‹ฐ ๋˜๋Š” ๋ฉ”์†Œ๋“œ๊ตฌํ˜„์„ ์œ„ํ•ด ์‚ฌ์šฉ๋จ. ( JS์—๋Š” ์—†๋‹ค. )

๐Ÿ‘‰ interface์™€ implements ํ‚ค์›Œ๋“œ

๋‘ ํ‚ค์›Œ๋“œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋กœ ์ปดํŒŒ์ผ ๋˜์ง€ ์•Š์Œ.
๊ฐœ๋ฐœ๋„์ค‘ ์ž˜๋ชป๋œ ํƒ€์ž…์„ ํ”ผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ์—ญํ• .

// 2_16.ts
interface Person{
    firstName: string;
    lastName: string;
    age: number;
}

๐Ÿ‘‰ class์™€ ์ฐจ์ด

์ปค์Šคํ…€ ํƒ€์ž…์„ class์™€ ๊ฐ™์ด ์„ ์–ธํ•˜๋ฉด ๋งˆ์น˜ ๊ฐ’์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ๋ฒˆ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
(์œ„์˜ new ํ‚ค์›Œ๋“œ ์ฒ˜๋Ÿผ ์ธ์Šคํ„ด์Šคํ™” ๊ฐ€๋Šฅ)

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ์— ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ํฌํ•จ์ด ๋œ๋‹ค. (ES5 - ํ•จ์ˆ˜, ES6 - ํด๋ž˜์Šค๋กœ ์ปดํŒŒ์ผ)

๐Ÿ‘‰ ์˜ˆ์ œ

interface Person {
    firstName: string;
    lastName: string;
    age: number;
}

function savePerson (person: Person): void{
    console.log('Saving ' , person);
}

const p:Person = {
    firstName: 'John',
    lastName: 'Smith',
    age: 25
};

savePerson(p);
"use strict";
function savePerson(person){
    console.log('Saving ' , person);
}
const p = {
    firstName: 'John',
    lastName: 'Smith',
    age: 25 
}
savePerson(p);

์ด์ฒ˜๋Ÿผ savePerson์— ๋“ค์–ด์˜จ ๊ฐ์ฒด๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค Person๋‚ด ํ”„๋กœํผํ‹ฐ์™€ ๋น„๊ตํ•˜๋ฉฐ ํ™•์ธํ•จ.

Personํƒ€์ž…์„ ์‚ญ์ œํ•ด๋„ savePersonํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ทธ ์ด์œ ๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ๊ตฌ์กฐ์  ํƒ€์ž… ์‹œ์Šคํ…œ ๋•Œ๋ฌธ

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ๋‘ ํƒ€์ž…์˜ ๊ตฌ์กฐ๋งˆ์„ ๊ฐ€์ง€๊ณ  ํ˜ธํ™˜์„ฑ์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. => ๋ฉค๋ฒ„๊ฐ€ ์„œ๋กœ ์ผ์น˜ํ•œ๋‹ค๋ฉด ์„œ๋กœ ํ˜ธํ™˜๋˜์–ด ๋ช…์‹œ์ ์ธ ํ‘œ์‹œ๋Š” ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๐Ÿ‘‰ type, interface, class ์–ธ์ œ ์‚ฌ์šฉ??

  • ๋Ÿฐํƒ€์ž„ ๋™์•ˆ ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ํ•œ๋‹ค๋ฉด interface ๋˜๋Š” type
    ๊ทธ ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ์—๋Š” class => ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ class
  • ํƒ€์ž… ๊ฒ€์‚ฌ๊ธฐ๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ์ปค์Šคํ…€ ํƒ€์ž…์„ ์„ ์–ธํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด type ๋˜๋Š” interface์‚ฌ์šฉ.
    type๊ณผ interface๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์ปดํŒŒ์ผ ๋˜์ง€ ์•Š์•„ ๋Ÿฐํƒ€์ž„ ์ฝ”๋“œ ์šฉ๋Ÿ‰์ด ๋” ์ž‘์•„์ง„๋‹ค.
  • typeํ‚ค์›Œ๋“œ๋Š” interface์™€ ๋™์ผํ•œ ๊ธฐ๋Šฅ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    ex) interface๋Š” ๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ ๊ฐœ๋…์„ ์‚ฌ์šฉ ๋ชปํ•˜์ง€๋งŒ type์€ ๊ฐ€๋Šฅํ•˜๋‹ค.

๐ŸŒ 2.2.4 ๊ตฌ์กฐ์  ํƒ€์ž… ์‹œ์Šคํ…œ๊ณผ ๋ช…๋ชฉ์  ํƒ€์ž… ์‹œ์Šคํ…œ

๋‘ ๊ฐœ์˜ ํƒ€์ž…์ด ์žˆ์„ ๋•Œ ๊ฐ™์€์ง€ ๋‹ค๋ฅธ์ง€ ์–ด๋–ป๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ?

๋ช…๋ชฉ์  ํƒ€์ž… ์‹œ์Šคํ…œ ( ์ž๋ฐ” ๋“ฑ )์„ ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด๋“ค์€ ๊ฐ™์€ ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์•ˆ์— ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์„ ์–ธ๋œ ํด๋ž˜์Šค๋ฅผ ๋™์ผํ•˜๋‹ค๊ณ  ํŒ๋‹จํ•จ.

์ฆ‰, Personํƒ€์ž… ๋ณ€์ˆ˜์—๋Š” Person ํƒ€์ž… ๊ฐ์ฒด๋‚˜ ์ด๋ฅผ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋งŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

// 2_17.java
class Person {
    String name;
}
class Customer {
    String name;
}
Customer cust = new Person(); // ํด๋ž˜์Šค ์ด๋ฆ„์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ๋˜์ง€ ์•Š๋Š”๋‹ค.

ํด๋ž˜์Šค ์ด๋ฆ„์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ๋˜์ง€ ์•Š๋Š”๋‹ค.

// 2_18.ts
class Person2_18{
    name: string;
}
class Customer2_18{
    name: string;
}
const cust:Customer2_18 = new Person2_18(); // ๊ฐ™์€ ๊ตฌ์กฐ๋ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ X

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ๊ตฌ์กฐ์  ํƒ€์ž… ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋–„๋ฌธ์— ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ ธ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ.

// 2_19.ts
class Person2_19{
    name: string;
}
class Customer2_19{
    name: string;
}
const cust:Customer2_19 = { name: 'Mary'};
const pers:Person2_19 = { name: 'John'};

๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•ด ๊ตฌ์กฐ๊ฐ€ ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ํด๋ž˜์Šค ํƒ€์ž… ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

โ— ์ ‘๊ทผ์ œ์–ด์ž๋Š” ํƒ€์ž… ํ˜ธํ™˜์„ฑ์— ์˜ํ–ฅ์„ ์ค๋‹ˆ๋‹ค.
name์ด private์ธ ๊ฒฝ์šฐ ์ปดํŒŒ์ผ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Person๊ณผ Customer ๊ตฌ์กฐ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด?

// 2_20.ts
class Person2_20{
    name: string;
    age: number;
}
class Customer2_20{
    name: string;
}
const cust:Customer2_20 = new Person2_20();

name์„ ๊ฐ€์ง„ Customerํƒ€์ž…์˜ ์ƒ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด name์„ ๊ฐ€์ง„ Person2_20์„ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜X

Person์ธ์Šคํ„ด์Šค๋„ name์ด ์กด์žฌํ•˜๋ฏ€๋กœ cust.name='John'์ด ๊ฐ€๋Šฅ.

โ— Customerํƒ€์ž…์ธ ๋ณ€์ˆ˜ ๊ฐ’์„ Person ํƒ€์ž…์ด ใ„ด๊ฐ์ฒด๋กœ ์ง€์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— Personํƒ€์ž…์€ Customerํƒ€์ž…์— ํ• ๋‹น ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜๋Œ€๋Š”?

// 2_21.ts
class Person2_21{
    name: string;
    
}
class Customer2_21{
    name: string;
    age: number;
}
const cust21:Customer2_21 = new Person2_21(); // ์˜ค๋ฅ˜ ๋ฐœ์ƒ

Person์•ˆ์— age๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ.

๐ŸŒ 2.2.5 ์ปค์Šคํ…€ ํƒ€์ž…์˜ ์œ ๋‹ˆ์˜จ

์œ ๋‹ˆ์˜จ์€ ๋ณ€์ˆ˜์— ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

// 2_22.ts - ๊ฐ ์•ก์…˜์ด js์Šคํƒ€์ผ๋กœ ์ฝ”๋”ฉ๋˜ 3์žฅ์—์„œ ๋ฆฌํŒฉํ† ๋ง ์˜ˆ์ •
export class SearchAction {
    actionType = "SEARCH";
    constructor(readonly payload: {searchQuery: string}){}
}

export class SearchSuccessAction{[1]
	actionType = 'SEARCH_SUCCESS';
	constructor(public payload: {searchResults: string[] }){}
}

export class SearchFailedAction {
    actionType = 'SEARCH_FAILED';
}

export type SearchActions = SearchAction | SearchSuccessAction | SearchFailedAction

์‹๋ณ„ ๊ฐ€๋Šฅํ•œ ์œ ๋‹ˆ์˜จ์€ ๊ณตํ†ต ํ”„๋กœํผํ‹ฐ ์ฆ‰, ๊ณตํ† ์œผ์ด ์‹๋ณ„์ž๋ฅผ ๊ฐ€์ง„ ๋ฉค๋ฒ„๋กœ ์ด๋ฃจ์–ด์ง„ ํƒ€์ž…์„ ๋งํ•จ.

๊ฐ ๋ฉค๋ฒ„๋งˆ๋‹ค actionType์ด๋ผ๋Š” ์‹๋ณ„์ž๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ ๋‹ˆ์˜จ ์‹๋ณ„์— ํ•ด๋‹นํ•จ.

// 2_23.ts
interface Rectangle {
    kind: 'rectangle';
    width: number;
    height: number;
}

interface Circle {
    kind: 'circle'; 1
	radius: number;
}

type Shape = Rectangle | Circle; // union

kind๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด kind๊ฐ’์— ๋”ฐ๋ผ shpae ํฌ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

// 2_24.ts
function area(shape: Shape):number {
    switch(shape.kind){
        case 'rectangle': return shape.height * shape.width;
        case 'circle': return Math.PI * shape.radius **2;
    }
}

const myRectangle:Rectangle = {kind: 'rectangle', width:10, height: 20}
console.log(`Rectangle's area is ${area(myRectangle)}`);

const myCircle:Circle = {kind: 'circle', radius: 10};
console.log(`Circle's area is ${area(myCircle)}`);

๐ŸŒ ํƒ€์ž…๊ฐ€๋“œ in

inํ‚ค์›Œ๋“œ๋Š” ํƒ€์ž… ๋ฒ”์œ„๋ฅผ ์ถ•์†Œํ•˜๋Š” ํ‘œํ˜„.

์œ ๋‹ˆ์˜จ ํƒ€์ž… ์ธ์ž๋ฅผ ๊ฐ€์ง„ ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœํ•˜๋Š” ๋™์•ˆ ์‹ค์ œ ๊ฐ’์„ ์ฒดํฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

interface A { a: number };
interface B { b: string };

function foo(x: A|B){
    if('a'in x){
        return x.a;
    }
    return x.b;
}
profile
๊นƒํ—ˆ๋ธŒ : github.com/JuneHyung

0๊ฐœ์˜ ๋Œ“๊ธ€