๐ฅ ์ง๋
3. ํ์ ์คํฌ๋ฆฝํธ ์ดํดํ๊ธฐ (6/9)
- ๊ฐ์ฒด ํ์ ์ ํธํ์ฑ
- ๋์ ํ์
- ํ์ ์ถ๋ก
๐ ์์ฝ !
๊ฐ์ฒด ํ์ ์ ํธํ์ฑ
// ๊ธฐ๋ณธ ํ์
๊ฐ์ ํธํ์ฑ
let num1: number = 10;
let num2: 10 = 10;
num1 = num2; // num2 = num1;
// ๊ฐ์ฒด ํ์
๊ฐ์ ํธํ์ฑ
// -> ์ด๋ค ๊ฐ์ฒด ํ์
์ ๋ค๋ฅธ ๊ฐ์ฒดํ์
์ผ๋ก ์ทจ๊ธํด๋ ๊ด์ฐฎ์๊น?!
type Animal = {
name: string;
color: string;
};
type Dog = {
name: string;
color: string;
breed: string;
};
let animal: Animal = {
name: "giraffe",
color: "yellow",
};
let dog: Dog = {
name: "syrious",
color: "black",
breed: "gray hound",
};
animal = dog; // dog = animal;
// ์ด๊ณผ ํ๋กํผํฐ๊ฒ์ฌ
type Book1 = {
name: string;
price: number;
};
let book2: Book1 = {
name: "dynosour book",
price: 52000,
// skill: "algorism",
};
let book3: Book1 = programmingBook;
function func(book: Book) {}
func({ name: "dynosour book",
price: 52000,
// skill: "algorism"
});
func(programmingBook);
๋์ ํ์
- ์ฌ๋ฌ๊ฐ์ ํ์ ์ ํฉ์ฑํด์ ์๋กญ๊ฒ ๋ง๋ค์ด๋ธ ํ์
- ํฉ์งํฉ ํ์ ๊ณผ ๊ต์งํฉ ํ์ ์ด ์กด์ฌํจ
/**
* 1. ํฉ์งํฉ Union Type
*/
let a: string | number | boolean;
a = 1;
a = "hello";
a = true;
type Dog = {
name: string;
color: string;
};
type Person = {
name: string;
language: string;
};
type Union1 = Dog | Person;
let union1:Union1 = {
name: "",
color: "",
}
let union2:Union1 = {
name: "",
language: "",
}
let union3: Union1 = {
name: "",
color: "",
language: "",
};
// Dog์ ์งํฉ๋, Person์ ์งํฉ๋ ์๋๋ union4๋ ๋ถ๊ฐ
// let union4: Union1 = {
// name: ""
// }
/**
* 2. ๊ต์งํฉ Intersection Type
*/
let veriable: number & string;
type Dog2 = {
name: string;
color: string;
};
type Person2 = {
name: string;
language: string;
};
type Intersection = Dog2 & Person2;
// A & B ์ ํ์
์ผ ๊ฒฝ์ฐ A์ B์ ๋ด์ฉ๋ฌผ์ด ํ๋๋ผ๋ ์์ผ๋ฉด ์๋๋ค ๋ชจ๋ ๋ค์ด๊ฐ ์์ด์ผ ํจ
let intersection: Intersection = {
name: "",
color: "",
language: "",
}
ํ์ ์ถ๋ก
/**
* ํ์
์ถ๋ก Type Inference
*/
let a = 10;
let b = "hello";
let c = {
id: 1,
name: "์ง์",
profile: {
nickname: "jieunw",
introduce: "nice to meet you",
},
urls: ["https://velog.io/@jieunwang"],
};
let { id, name, profile } = c;
let [one, two, three] = [1, "hello", true];
// ํจ์์ ๋ฐํ๊ฐ ํ์
์ ์ถ๋ก ํ ๋๋ ์ด๊ธฐํํ๋ ๊ฐ์ด ์๋
// return๋ฌธ ๋ค์์ ๋ฐํ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ถ๋ก ํจ
function func(message = "hello") {
return "hello";
}
let d; // ์๋ฌต์ any ์ถ๋ก . ๋ช
์์ ์ผ๋ก let d:any ๋ก ์ ์ํ ๋๋ ์งํ ๊ฐ์ ๊ฒ ์์ด ๊ณ์ anyํ์
์ธ๋ฐ, ์๋ฌต์ any๋ฉด ํ์
์งํํจ
d = 10; // <- any ์์ number๋ก
d.toFixed(1);
d = "hi"; // string์ผ๋ก
d.toUpperCase();
// Evolution of any type. ์ด๊ธฐ๊ฐ์ ์ง์ ํ์ง ์์ผ๋ฉด ์๋ฌต์ ์ผ๋ก ์งํํ ์ ์๋ค. ํ์ง๋ง ์ข์ง ์์ ๋ฐฉ๋ฒ
// ๋์ค์ ์์๋ง์ถฐ์ผํ๋ ์ํฉ์ด ์ฌ ์๋ ์์ผ๋ ์ฌ์ฉํ๋ ๊ฑธ ์ถ์ฒํ์ง ์์
const num = 10;
const differenceOfConst = "AAAAA"; // const differenceOfConst: "AAAAA"
let differenceOfLet = "BB"; //let differenceOfLet: string
let any = [1, "string"]; // let any: (string | number)[]
// ํ์
์คํฌ๋ฆฝํธ๋ ์ฌ๋งํ ๋ณ์๋ ๋ชจ๋ ํ์
์ถ๋ก ํด์ค
// = > const๋ก ์ ์ธ๋ ๋ณ์๊ฐ ์๋๋ฉด ํ์
๋ํ๊ธฐ๋ฅผ ํตํด ํ์
์ ์ถ๋ก ํด์ค๋ค
๐ดโ ๏ธ ๊ณผ์
Q1. ์๋ ์ฝ๋์ 4๊ฐ์ ๋ณ์ a,b,c,d์ ํ์ ์ ๊ฐ๊ฐ ์ด๋ป๊ฒ ์ถ๋ก ๋ ๊น์?
let a = 10;
const b = 20;
const c = [1, 2];
const d = [1, "hello", true];
const e = [1, 2, 3] as const;
/* 1-1 ๋ณ์ a์ ํ์
์ ๋ฌด์์ผ๋ก ์ถ๋ก ๋ ๊น์? */
type A = any;
/* 1-2 ๋ณ์ b์ ํ์
์ ๋ฌด์์ผ๋ก ์ถ๋ก ๋ ๊น์? */
type B = any;
/* 1-3 ๋ณ์ c์ ํ์
์ ๋ฌด์์ผ๋ก ์ถ๋ก ๋ ๊น์? */
type C = any;
/* 1-4 ๋ณ์ d์ ํ์
์ ๋ฌด์์ผ๋ก ์ถ๋ก ๋ ๊น์? */
type D = any;
/* 1-5 ๋ณ์ e์ ํ์
์ ๋ฌด์์ผ๋ก ์ถ๋ก ๋ ๊น์? */
type E = any;
import { Equal, Expect, NotAny } from "@type-challenges/utils";
type TestCases = [
Expect<Equal<A, number>>,
Expect<Equal<B, 20>>,
Expect<Equal<C, number[]>>,
Expect<Equal<D, (number | string | boolean)[]>>,
Expect<Equal<E, [1, 2, 3]>>
];
์ ์ถ โก๏ธ
1-1 ) type A = number;
1-2 ) type B = 20;
1-3 ) type C = number[];
1-4 ) type D = (number | string | boolean)[];
1-5 ) type E = [1,2,3];
Q2. ๋ค์ ์๊ตฌ์ฌํญ์ ๋ง์กฑํ๋ Animal, DogCat(๊ฐ๋ฅ์ด) ํ์ ์ ์์ฑํ์ธ์.
- Animal ํ์ ์ Dog ํ์ ์ผ ์๋ Cat ํ์ ์ผ ์๋ ์์ต๋๋ค.
- DogCat ํ์ ์ Dog์ด์ Cat์ ๋๋ค.
/* ์๋์ ์ฝ๋๋ฅผ ์์ฑํด ์ค๋ฅ๋ฅผ ์ ๊ฑฐํ์ธ์ */
type Dog = {
name: string;
color: string;
};
type Cat = {
name: string;
age: number;
};
type Animal = never;
type DogCat = never;
/* [Test] ์ฌ๊ธฐ๋ถํฐ๋ ์ ๋ต์ ์ฒดํฌํ๊ธฐ ์ํ ์ฉ๋๋ก ์์ ํ์ง ์์ต๋๋ค */
const animals: Animal[] = [
{
name: "ํ ๋ญ์ด",
color: "brown",
},
{
name: "์จ๋ฏธ",
age: 10,
},
];
const dogCat: DogCat = {
name: "๊ฐ๋ฅ์ด",
age: 2,
color: "์น์ฆ์",
};
์ ์ถ โก๏ธ
type Dog = {
name: string;
color: string;
};
type Cat = {
name: string;
age: number;
};
type Animal = Dog | Cat;
type DogCat = Dog & Cat;