/typescript/

TypeScript part 10 (function type)

2017-03-04 12:52:58

Without function type

TypeScript

function multiply(val1: number, val2: number): number {
    return val1 * val2;
}

function connect(val1: string, val2: string): string {
    return val1 + val2;
}

let myFunction;

myFunction = multiply;
console.log(myFunction(1, 1));

myFunction = connect;
console.log(myFunction("1", "1"));

JavaScript

function multiply(val1, val2) {
    return val1 * val2;
}
function connect(val1, val2) {
    return val1 + val2;
}
var myFunction;
myFunction = multiply;
console.log(myFunction(1, 1));
myFunction = connect;
console.log(myFunction("1", "1"));

With function type

TypeScript

function multiply(val1: number, val2: number): number {
    return val1 * val2;
}

function connect(val1: string, val2: string): string {
    return val1 + val2;
}

let myFunction: (val1: number, val2: number)=> number;

myFunction = multiply;
console.log(myFunction(1, 1));

// myFunction = connect;
// console.log(myFunction("1", "1"));

JavaScript

function multiply(val1, val2) {
    return val1 * val2;
}
function connect(val1, val2) {
    return val1 + val2;
}
var myFunction;
myFunction = multiply;
console.log(myFunction(1, 1));
// myFunction = connect;
// console.log(myFunction("1", "1"));