Skip to content

Instantly share code, notes, and snippets.

@GerardKetuma
Last active February 28, 2020 19:50
Show Gist options
  • Save GerardKetuma/d303bbeb527faacdc61d7c554dcdf2c2 to your computer and use it in GitHub Desktop.
Save GerardKetuma/d303bbeb527faacdc61d7c554dcdf2c2 to your computer and use it in GitHub Desktop.
Functions in TypeScript
// See blog post at https://ketuma.com/lessons/typescript-functions
// Functions
// Fully Typed Function
const multiply: (str: string, num: number) => string = function(str: string, num: number): string {
return `${str} `.repeat(num).trim()
}
console.log(multiply('Jake', 3)) // Jake Jake Jake
// Break out the type
interface IMultiply {
(str: string, num: number): string
}
const multiply: IMultiply = function(str: string, num: number): string {
return `${str} `.repeat(num).trim()
}
console.log(multiply('Jake', 3)) // Jake Jake Jake
// Infer function type from function definition
const multiply = function(str: string, num: number): string {
return `${str} `.repeat(num).trim()
}
console.log(multiply('Jake', 3)) // Jake Jake Jake
// Contextual typing inference
const multiply: (str: string, num: number) => string = function(str, num) {
return `${str} `.repeat(num).trim()
}
console.log(multiply('Jake', 3)) // Jake Jake Jake
//------------------------------------------------------------------------
interface IMultiply {
(str: string, num?: number): string
}
const multiply: IMultiply = function(str: string, num = 1): string {
return `${str} `.repeat(num).trim()
}
console.log(multiply('Jake')) // Jake
//------------------------------------------------------------------------
interface IShowNames {
(name: string, ...rest: string[]): string
}
const showNames: IShowNames = function(name: string, ...rest: string[]): string {
return `${name} ${rest.join(' ')}`.trim()
}
console.log(showNames('Jake')) // Jake
console.log(showNames('Jake', 'Paul', 'Morgan')) // Jake Paul Morgan
//------------------------------------------------------------------------
let stocks = [
{ stock: 'AAPL', name: 'Apple', price: 150 },
{ stock: 'FB', name: 'Facebook', price: 84 },
{ stock: 'MSFT', name: 'Microsoft', price: 124 },
{ stock: 'GOOG', name: 'Google', price: 650 },
{ stock: 'WFC', name: 'Wells Fargo', price: 45 },
{ stock: 'GE', name: 'General Electric', price: 12 },
]
function pickStocks(x: string): {stock: string, name: string, price: number}
function pickStocks(x: number): {stock: string, name: string, price: number}
function pickStocks(x: string | number) {
if (typeof x === 'string') {
return stocks.find(stock => stock.name === x)
} else if (typeof x === 'number') {
return stocks[Math.floor(Math.random() * stocks.length)]
}
}
console.log(pickStocks('Apple')) // { stock: 'AAPL', name: 'Apple', price: 150 }
console.log(pickStocks(43)) // Random value: { stock: 'GOOG', name: 'Google', price: 650 }
console.log(pickStocks([23])) // Error: No overload matches this call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment