July 2017
Intermediate to advanced
300 pages
5h 43m
English
We can set constraints on a function by using the function type literal:
const showModal: (toggle: boolean) => void =
function( toggle ) {
console.log( toggle );
}
I find it quite discouraging and prefer to use interface:
interface Switcher {
(toggle: boolean): void;
}
const showModal:Switcher = ( toggle ) => {
console.log( toggle );
}
showModal( true );
You may now ask, what if the function has optional parameters? TypeScript makes it very simple to define an optional parameter. You just need to append the parameter with a question mark:
function addOgTags(title: string, description?: string): string { return ` <meta property="og:title" content="${title}" /> <meta property="og:description" content="${description || ""}" ...Read now
Unlock full access