Skip to content

Instantly share code, notes, and snippets.

@joshuaatencia
Last active March 25, 2021 19:43
Show Gist options
  • Save joshuaatencia/421c4e417055526e82cc21a012a4f4a3 to your computer and use it in GitHub Desktop.
Save joshuaatencia/421c4e417055526e82cc21a012a4f4a3 to your computer and use it in GitHub Desktop.
function with diferent varial's type
type Combinable = number | string;
type ConversionDescriptor = 'as-number' | 'as-text';
function combine(input1: Combinable, input2: Combinable, resultConversation: ConversionDescriptor){
let result;
if(typeof input1 === 'number' && typeof input2 === 'number' || resultConversation === 'as-number'){
result = +input1 + +input2;
}else{
result = `${input1} ${input2}`;
}
return result;
}
const combinedStringAge = combine(12,14, 'as-number')
console.log(combinedStringAge);
const age = combine(12, 14, 'as-number');
console.log(age)
const names = combine('Daniela', 'Romero', 'as-text');
console.log(names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment