Una función en Typescript puede recibir como parámetro diferentes tipos predefinidos usando Union Types
function whatTime(hour: number | string, minute: number | string) : string {
return hour+':'+minute;
}
whatTime(1,30) //'1:30'
whatTime('1',30) //'1:30'
whatTime(1,'30') //'1:30'
whatTime('1','30') //'1:30'
Usando Alias podemos reducir la cantidad de código en los tipos predefinidos.
type Hours = number | string;
type Minutes = number | string;
function whatTime(hour: Hours, minute: Minutes) : string {
return hour+':'+minute;
}
¿Quieres ver más aportes, preguntas y respuestas de la comunidad? Crea una cuenta o inicia sesión.