08. ๐ฆ ์ฌ์ฉ์ ์ ์ ํ์ ๊ฐ๋
๐ 0.typeof, instanceof?
๐ 1. typeof
โ ๊ฐ๋ : ๊ธฐ๋ณธ ํ์ (number, string, boolean, undefined, symbol, bigint, function, object)์ ๊ตฌ๋ถํ ์ ์์
์์ ๊ฐ(primitive value)์ ์ฃผ๋ก ์ฌ์ฉ
typeof 123; // "number"
typeof "hello"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof Symbol(); // "symbol"
typeof BigInt(1); // "bigint"
typeof function () {}; // "function"
typeof {}; // "object"
typeof null; // "object" โ๏ธ (๋ฒ๊ทธ์ด์ ์ญ์ฌ์ ์ ์ฐ)
๐ 2. instanceof
โ ๊ฐ๋ : ๊ฐ์ฒด๊ฐ ์ด๋ค ์์ฑ์ ํจ์์ ์ธ์คํด์ค์ธ์ง ํ์ธํ ๋ ์ฌ์ฉ
ํ๋กํ ํ์ ์ฒด์ธ์ ๋ฐ๋ผ ํ์ธ
โ ์ฌ์ฉ ์:
class Person {}
const p = new Person();
p instanceof Person // true
[] instanceof Array // true
[] instanceof Object // true (Array๋ Object๋ฅผ ์์)
null instanceof Object // false โ๏ธ
function Dog() {}
const d = new Dog();
d instanceof Dog // true
๐ 1. ํ์ ๊ฐ๋๋?
- ํ์
์คํฌ๋ฆฝํธ์์ ์ ๋์จ ํ์
์ ๋ถ๊ธฐํด์ ์์ ํ๊ฒ ์ขํ๋ ๋ก์ง
- ๋ํ์ ์ธ ์: typeof, instanceof, โpropโ in obj
๐ 2. ์ฌ์ฉ์ ์ ์ ํ์ ๊ฐ๋ (Custom Type Guard)
๊ธฐ๋ณธ ํ์ ๊ฐ๋
interface Cat {
meow: number;
}
interface Dog {
bow: number;
}
// ๊ธฐ๋ณธ ํ์
๊ฐ๋
// a is Dog ํจ์๊ฐ true๋ฅผ ๋ฐํํ ๊ฒฝ์ฐ, a๋ Dog ํ์
์ด๋ผ๊ณ ํ๋จ
function catOrDog(a: Cat | Dog): a is Dog {
// ํ์
ํ๋ณ์ ํ ์ ์๋ ํจ์
if ((a as Cat).meow) {
return false;
}
return true;
}
const cat: Cat | Dog = { meow: 3 };
if (catOrDog(cat)) {
console.log(cat.meow);
}
if ("meow" in cat) {
console.log(cat.meow);
}
๐ 3. PromiseSettledResult<T>
ํํฐ๋ง
๐น PromiseSettledResult๋?
type PromiseSettledResult<T> =
| PromiseFulfilledResult<T>
| PromiseRejectedResult
- .allSettled()์์ ์ฌ์ฉ๋จ
- ๋ชจ๋ Promise๊ฐ ๋๋ ๋๊น์ง ๊ธฐ๋ค๋ฆผ
- ์ฑ๊ณตํด๋, ์คํจํด๋ ์ ๋ถ ๋ฐํํจ โ
- ๊ฒฐ๊ณผ ๋ฐฐ์ด์ ๋ค์๊ณผ ๊ฐ์ ํํ:
- ์ฑ๊ณต : { status: "fulfilled", value: 1 },
- ์คํจ : { status: "rejected", reason: "์๋ฌ" },
- ์ฑ๊ณต โ { status: "fulfilled", value: T }
- ์คํจ โ { status: "rejected", reason: any }
โ ์คํจํ ๊ฒฐ๊ณผ๋ง ํํฐ๋ง
// ์คํจํ๊ฒ๋ง ๊ฐ์ ธ์ค๊ณ ์ถ์๋
const isRejected = (input: PromiseSettledResult<unknown>): input is PromiseRejectedResult =>
input.status === "rejected";
โ ์ฑ๊ณตํ ๊ฒฐ๊ณผ๋ง ํํฐ๋ง
const isFulfilled = <T>(input: PromiseSettledResult<T>): input is PromiseFulfilledResult<T> =>
input.status === "fulfilled";
๐ 4. Promise ์ํ ์์ฝ
์ํ | ์ค๋ช |
---|---|
pending | ์์ง ์คํ ์ค |
fulfilled | ์ฑ๊ณต ์๋ฃ โ .then() |
rejected | ์คํจ ์๋ฃ โ .catch() |
settled | ์๋ฃ๋จ (fulfilled or rejected) |
Promise์ ์ํ
- Panding
- settled
- ์ฑ๊ณต : resolved
- ์คํจ : rejected
- settled
// Promise
// ์ฑ๊ณต resolved
// ์คํจ rejected
// sattled : .then().catch()
// ์ฑ๊ณต resolved .then()
// ์คํจ rejected .catch()
const results: PromiseSettledResult<number>[] = await Promise.allSettled([Promise.resolve(1), Promise.reject("err")]);
const successOnly = results.filter(isFulfilled); // ํ์
์ PromiseFulfilledResult<number>[]
const failureOnly = results.filter(isRejected); // ํ์
์ PromiseRejectedResult[]
promise.then().catch();
const getUser = (id: number) => {
if (id === 2) Promise.reject("โ ์คํจ");
else Promise.resolve({ id, name: `User ${id}` });
};
const requests = [1, 2, 3].map(getUser);
const results = await Promise.allSettled(requests);
//[
// { status: "fulfilled", value: { id: 1, name: "User 1" } },
// { status: "rejected", reason: "โ ์คํจ" },
// { status: "fulfilled", value: { id: 3, name: "User 3" } }
//]
Last updated on