Typescript Flashcards
(10 cards)
Como tipar props de componente React?
type Props = { title: string }
const MyComponent: React.FC<Props> = ({ title }) => ...</Props>
Qual a diferença entre interface e type?
Ambos definem formas de objeto, mas interface permite extensão.
Como tornar uma prop opcional?
interface Props { idade?: number }
O que é uma union type?
Permite múltiplos tipos possíveis.
type Status = ‘ativo’ | ‘inativo’
Como usar genéricos?
function identity<T>(arg: T): T {
return arg;</T>
O que é Record em TS?
Mapeia chaves para tipos.
Record<string, number>
Como tipar funções?
const soma: (a: number, b: number) => number = (a, b) => a + b;
Como lidar com objetos com chaves dinâmicas?
type Obj = { [key: string]: number }
Como forçar um valor como um tipo?
const valor = “ok” as “ok”;
Como tipar estados com useState?
const [count, setCount] = useState<number>(0);</number>