DEV Community

Cristian Fernando
Cristian Fernando

Posted on • Updated on

 

Paracetamol.ts馃拪| #03: Explica este c贸digo TypeScript

Explica este c贸digo TypeScript

Dificultad: B谩sico

enum Helados{
  Crema = 10,
  Agua,
}

let miHeledo: Helados = Helados.Agua;
console.log(miHeledo)
Enter fullscreen mode Exit fullscreen mode
  • A. 10
  • B. 11
  • C. ReferenceError
  • D. RangeError

Respuesta en el primer comentario.


Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando • Edited

Respuesta:
鉁 B. 11

Los enum por defecto empiezan en 0, tal cual como si fueran un arreglo; lo interesante es que podemos modificar este comportamiento asignando un index arbitrario que reemplace al valor 0 inicial.

En este caso tenemos Crema = 10, y por ende el siguiente valor ser谩 Agua = 11.

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it鈥檚 surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!