React + Typescript

Note

Course ini ngebahas penggunaan typescript di aplikasi react. Buat belajar lebih dalam, ada course khusus yang bahas typescript

Typescript learning path : https://frontendmasters.com/learn/typescript/


Introduction - Kenapa pakai typescript?


Contoh penggunaan

Define type props

Sebisa mungkin jangan pakai any tapi kalau mentok gpp :). Kalau mau opsional, bisa tambahin ? di props nya.

import { ChangeEventHandler } from 'react';

type ControlPanelProps = {
  name: string;
  onChange: React.ChangeEventHandler<HTMLInputElement>;
  description?: string; // optional
  size: 'normal' | 'large' // Option / variant
};

const ControlPanel = ({ name, onChange, description }: ControlPanelProps) => {
  // …
};

Kalau bingung sama type sebuah props coba hover props nya. Nanti muncul popup type nya. Khusus pake VS Code. Kalau yang lain belum tau bisa apa ngga.

Pasted image 20231101090101.png

Props yang sering dipakai
https://fem-react-typescript.vercel.app/Commonly-used props.md


Types vs Interface

Sebuah panduan kapan pakai type atau interface
https://fem-react-typescript.vercel.app/Types versus interfaces.md

DwV-oOsXcAIct2q.jpg