Typescript Flashcards

(10 cards)

1
Q

Como tipar props de componente React?

A

type Props = { title: string }
const MyComponent: React.FC<Props> = ({ title }) => ...</Props>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Qual a diferença entre interface e type?

A

Ambos definem formas de objeto, mas interface permite extensão.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Como tornar uma prop opcional?

A

interface Props { idade?: number }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

O que é uma union type?

A

Permite múltiplos tipos possíveis.

type Status = ‘ativo’ | ‘inativo’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Como usar genéricos?

A

function identity<T>(arg: T): T {
return arg;</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

O que é Record em TS?

A

Mapeia chaves para tipos.
Record<string, number>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Como tipar funções?

A

const soma: (a: number, b: number) => number = (a, b) => a + b;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Como lidar com objetos com chaves dinâmicas?

A

type Obj = { [key: string]: number }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Como forçar um valor como um tipo?

A

const valor = “ok” as “ok”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Como tipar estados com useState?

A

const [count, setCount] = useState<number>(0);</number>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly