TR Output

フロントエンドエンジニアの備忘録

TypeScript

【TypeScript】型安全にJSON.parse()を使う

function safeJsonParse<T>(jsonString: string): T | null { try { const parsedData = JSON.parse(jsonString); if (typeof parsedData === 'object' && parsedData !== null) { return parsedData as T; } } catch (error) { console.error("JSONパースエラ</t>…

【TypeScript】型をすべてオプショナルに変換する

type SampleType = { id: number; name: string; } // 1 type OptionalSampleType1 = Partial<SampleType>; // 2 type OptionalSampleType2 = { [K in keyof SampleType]?: SampleType[K]; }</sampletype>

Next.js + TypeScript 環境構築メモ

Next.js (+TypeScript) の環境構築 create-next-app のインストール npm i -g create-next-app プロジェクトの作成 npx create-next-app@latest --typescript 実行 npm run dev ESLint の設定 パッケージのインストール npm i -D eslint 設定 npx eslint --i…

【React × TypeScript】HTML属性の型情報のインポート方法

ReactとTypeScriptでカスタムコンポーネントを作っていた際に、通常のHTMLタグの属性の型をどのように持ってくればいいんだろうと思ったので、調べたことをまとめました。 結論:React.ComponentPropsWithoutRef ※もしくは React.ComponentPropsWithRef 以下…