So I was reading the documentation of Typescript, I know it is boring and annoying but sometimes we must be strong with ourselves and try to enjoy ourselves. Basically, Typescript is JavaScript but typed, it helps you to optimize your development skills and it doesn’t change your code, no external libraries. So when you have a mistake in Typescript ask yourself the same question but in JavaScript.
Here official documentation
Typescript supports primitive JavaScript ones with the addition of :
You can create complex types combining simple ones, you can use Unions or Generics to do so:
Unions: is represented by “|” operator that means “or”. It says that the value can be for example, a string “or” a boolean “or” an array. It is your custom type.
type WindowStates = "open" | "closed" | "minimized";
type LockStates = "locked" | "unlocked";
type OddNumbersUnderTen = 1 | 3 | 5 | 7 | 9;
Generics: as I understand generics create variables where you can host your type, so in the case of an array, which has multiple elements inside there can be anything. with generic, you specify the types inside the array.
type StringArray = Array<string>;
type NumberArray = Array<number>;
type ObjectWithNameArray = Array<{ name: string }>;
[ES]
TypeScript es compatible con JavaScript primitivo con la adición de:
Puede crear tipos complejos combinando tipos simples, puede usar Unions o Generics para hacerlo:
** Uniones: ** está representado por “|” operador que significa “o”. Dice que el valor puede ser, por ejemplo, una cadena “o” un booleano “o” una matriz. Es su ** tipo personalizado. **
`` jsx
escriba WindowStates = “abrir” | “cerrado” | “minimizado”;
escriba LockStates = “bloqueado” | “desbloqueado”;
tipo OddNumbersUnderTen = 1 | 3 | 5 | 7 | 9;
’’
** Genéricos: según tengo entendido, los genéricos crean variables donde puede alojar su tipo, por lo que en el caso de una matriz, que tiene varios elementos dentro, puede haber cualquier cosa. con genérico, especificas los tipos dentro de la matriz. **
`` jsx
type StringArray = Array <cadena>;
type NumberArray = Array <número>;
escriba ObjectWithNameArray = Array <{nombre: cadena}>;
’’