https://youtu.be/g9FL8hKoNqE?si=tsCYUjwOX5Z0AqZk
๋ถ์
Typescript ์ต์ ์ผ๋ก ์ฑ๋ฅ ๋ฌธ์ ์ง๋จ
extendedDiagnostics๋ก ๋๋ต์ ์ธ ์ปดํ์ผ ์๊ฐ ํ์ธ ๊ฐ๋ฅgenerateTrace๋ก ํธ๋ ์ด์ค ํ์ผ์ ์ถ๋ ฅํ ์ ์์. ์ด ํ์ผ์ ํด์ ๋ฃ์ด ํ์ธ ๊ฐ๋ฅ- trace.json : ์ปดํ์ผ์ ์์๋ ์๊ฐ ์ค ์ค๋ ๊ฑธ๋ฆฌ๋ ๋ถ๋ถ์ ์ถ๋ ฅ.
- types.json : ์ปดํ์ผ๋ฌ๊ฐ ์ธ์ํ ํ์ ์ ๋ชฉ๋ก์ ์ถ๋ ฅ.
ํด์ ์ฌ์ฉํด์ ์ฑ๋ฅ ๋ฌธ์ ์ง๋จ
@typescript/analyze-trace: CLI๋ก trace๋ฅผ ๋ถ์ํ์ฌ hot spot ์๋ณ ๊ฐ๋ฅ- https://ui.perfetto.dev/ : GUI๋ก ์์ธํ ํ์ธ์ด ๊ฐ๋ฅ
๊ฐ์
keyof JSX.IntrinsicElements;์ 175๊ฐ์ ํ๊ทธ ์ด๋ฆ์ด ๋ค์ด๊ฐ union type. ๋ฐ๋ผ์ Row๋ฅผ ์ ์ํ๋ ์๊ฐ 175๊ฐ์ ํ๊ทธ๋ฅผ ์ ์ํ ์ ์๋ ํ์
์ด ์ ์๋จ
type Elements = keyof JSX.IntrinsicElements;
type FlexProps<E extends Elements> = E extends Elements
? JSX.IntrinsicElements & { as?: E; gap?: number;}
: never;
export const Row = <E extends Elements>({ as, ...props }:
PropsWithChildren<FlexProps<E>>) => {
const &Element = as as React.ElementsType
return <$Element {...props} />;
}์์์ ํด๊ฒฐ์ฑ ์ผ๋ก ๋๊ฐ์ง ๋ฐฉ๋ฒ์ ์ ์ํจ
Type Narrowing
์ผ๋ถ ํ๊ทธ๋ง ์ง์ ํ์ฌ ํ์ ์ ๋ฒ์๋ฅผ ์ค์
type Elements = "div" | "section" | "label";Dynamic inference
์ ๋ค๋ฆญ์ ํตํด ํ์ํ ํ๊ทธ๋ง ์ง์ ํด์ ์ฌ์ฉํ ์ ์๋๋ก ํจ. Type Narrowing ๋ณด๋ค ๋ค์ํ ํ๊ทธ์ ๋์๊ฐ๋ฅ
type Elements = keyof JSX.IntrinsicElements;
type ElementType<P = any> = {
[K in Elements]: P extends JSX.IntrinsicElements[K]
}[Elements] | ((props: P) => JSX.Element);
type ElementProps<E extends ElementType> = E extends Elements ? JSX.IntrinsicElements[E] : {};
type FlexProps<E extends ElementType> = {as?: E};
type Props<E extends ElementType> = FlexProps<E> & Omit<ElementProps<E>, keyof FlexProps<E>>;
export const Row = <E extends ElementType>({ as, ...props }: Props<E>) => {
const &Element = as || "div";
return <$Element {...props} />;
}์ ์ฝ๋๋ฅผ ์๋์ ๊ฐ์ด ๊ฐ์ ํ ์ ์์
import { ComponentProps, ElementType } from "react";
type FlexProps<E extends ElementType> = {as?: E};
type Props<E extends ElementType> = FlexProps<E> & Omit<ComponentProps<E>, keyof FlexProps<E>>;
export const Row = <E extends ElementType>({ as, ...props }: Props<E>) => {
const &Element = as || "div";
return <$Element {...props} />;
}๊ทธ์ธ ๊ฐ์ ๋ฒ
ํ์ ์คํฌ๋ฆฝํธ์์ ๊ถ์ฅํ๋ ์ปดํ์ผ ๋๊ธฐ ์ฌ์ด ์ฝ๋ ์์ฑ๋ฒ (๋งํฌ)
- Preferring Interfaces Over Intersections
- Using Type Annotations
- Preferring Base Types Over Unions
- Naming Complex Types