InstruçÔes

Passo 4

Em uma palestra anterior, vocĂȘ aprendeu que inferĂȘncia de tipo Ă© a capacidade do TypeScript de determinar e atribuir automaticamente tipos a variĂĄveis, retornos de funçÔes e expressĂ”es com base no uso e contexto. No TypeScript, generics tornam funçÔes, classes e tipos mais flexĂ­veis. Generics permitem a criação de variĂĄveis de tipo, eliminando a necessidade de definir explicitamente os tipos a cada vez. Isso possibilita que a mesma função funcione com segurança com diferentes tipos. Aqui estĂĄ um exemplo simples de generics:
function Items<T>(list:T[]): T[] {
    return list
 }

Items<string | number>(["apple", 12,"orange"]) // here the argument is a union type
Functions can use two or more generics. T is used by convention, and additional type variables like S can be added as needed.
const SayHello = <S, T>(v1: S, v2: T): [S, T] => {
  return [v1, v2]
}

SayHello<string, string>("I'm", "alive") // ["I'm", "alive"]
SayHello<string, number>("I'm", 20)     // ["I'm", 20]
Create a function getElement that takes selector of type string as a parameter. It should also take a generic type T that extends HTMLElement, and have a return type T.

O que fazer:

Testes:

  • You should have an arrow function named `getElement`.
  • Your `getElement` function should take a parameter named `selector` of type `string`.
  • The generic type parameter `T` should extend the `HTMLElement` type.
  • Your `getElement` function should have a return type of `T`.

Preview