40
loading...
This website collects cookies to deliver better user experience
:
) and the type. First, you define a parameter. Then, you add colon right after it. After that, you specify what type is allowed for that specific parameter.// Create a function that accepts two parameters: str1 and str2.
// Both parameters must be of type string.
function joinStrings(str1: string, str2: string) {
return str1 + str2
}
// This will work:
joinStrings('Wo', 'rld')
// Output:
// 'World'
// This will not work:
joinStrings('Hel', 135)
// TS error: Argument of type 'number' is not assignable to parameter of type 'string'.
|
to separate them.// Create a function that accepts one parameter.
// This parameter can be either number, string or boolean.
function numStrBool(val: number | string | boolean) {
return typeof val
}
// This will work in TypeScript:
numStrBool(15)
// Output:
// 'number'
// This will also work in TypeScript:
numStrBool('Tea')
// Output:
// 'string'
// This will also work in TypeScript:
numStrBool(true)
// Output:
// 'boolean'
// This will not work in TypeScript:
numStrBool({ key: 15 })
// TS error: Argument of type '{ key: number; }' is not assignable to parameter of type 'string | number | boolean'.
?
) right after it. When you work with functions in TypeScript, the question mark goes after the parameter name and before the colon and type.// Create a function that accepts two parameters,
// the first one required and the second optional:
function combineWords(word1: string, word2?: string) {
return `${word1}${word2 ? word2 : ''}`
}
// This will work in TypeScript:
combineWords('Hello', 'world')
// Output:
// 'Helloworld'
// This will also work in TypeScript:
combineWords('Say', 'Bye')
// Output:
// 'SayBye'
// This will also work in TypeScript:
combineWords('Hi')
// Output:
// 'Hi'
// This will not work in TypeScript:
combineWords()
// TS error: Expected 1-2 arguments, but got 0.
// Example 1: default parameters and implicit type:
// Create a function that accepts two parameters.
// Both parameters are of type numbers,
// the second has default value 0.
function addTwoNumbers(num1: number, num2 = 0) {
return num1 + num2
}
// TS will add type for num2 implicitly:
// addTwoNumbers(num1: number, num2: number = 0)
// This will work in TypeScript:
addTwoNumbers(98, 656)
// Output:
// 754
// This will also work in TypeScript:
addTwoNumbers(63)
// Output:
// 63
// This will not work in TypeScript:
addTwoNumbers('13')
// TS error: Argument of type 'string' is not assignable to parameter of type 'number'.
// Example 2: default parameters and explicit type:
function addTwoNumbers(num1: number, num2: number = 0) {
return num1 + num2
}
type[]
or Array<type>
.// Create a function that accepts two parameters.
// The first parameter is a string.
// The second is a rest parameter and is an array of strings.
function buildATeam(leader: string, ...teammates: string[]) {
return `Team is composed of ${leader} as a the team leader and ${teammates.length > 0 ? teammates.join(', ') : 'nobody'} as the core team.`
}
// This will work in TypeScript:
buildATeam('Joe', 'Vicky', 'Thomas')
// Output:
// 'Team is composed of Joe as a the team leader and Vicky, Thomas as the core team.'
// This will also work in TypeScript:
buildATeam('Sandra', 'Alex', 'Jack', 'Victor')
// Output:
// 'Team is composed of Sandra as a the team leader and Alex, Jack, Victor as the core team.'
// This will also work in TypeScript:
buildATeam('Stuart')
// Output:
// 'Team is composed of Stuart as a the team leader and nobody as the core team.'
// This will not work in TypeScript:
buildATeam(13, 15)
// TS error: Argument of type 'number' is not assignable to parameter of type 'string'.
// Create a function that accepts two parameters.
// Both parameters are numbers.
// Set the return type to be a number (the '): number {' part).
function numToPow(num: number, exp: number): number {
return num ** exp
}
// This will work in TypeScript:
numToPow(15, 8)
// Output:
// 2562890625
// This will not work in TypeScript:
numToPow(12, '9')
// TS error: Argument of type 'string' is not assignable to parameter of type 'number'.
// Create function type for function subtract.
// This function accepts two parameters, both are numbers.
// Its return type is also a number.
let subtract: (a: number, b: number) => number
// Assign the actual function to the variable:
subtract = function (a, b) {
return a - b
}
// This will not work in TypeScript:
subtract = function (a: string, b: string) {
return a + b
}
// TS error: Type '(a: string, b: string) => string' is not assignable to type '(a: number, b: number) => number'.
// Create function type for function subtract:
let subtract: (a: number, b: number) => number
// These types are redundant:
subtract = function (a: number, b: number) {
return a - b
}
// Create function with function:
let subtract: (a: number, b: number) => number = function (a: number, b: number) {
return a - b
}
type
keyword. Second, you will replace the colon after variable name with equal sign. You are basically creating a new type and assign it a function signature.// Function type with variable:
let subtract: (a: number, b: number) => number
// Function type with type:
type TakeNumsReturnNums = (a: number, b: number) => number
// Use the "TakeNumsReturnNums" type with function expression:
const subtract: TakeNumsReturnNums = (a, b) => a - b
// This will work in TypeScript:
subtract(97, 13)
// Output:
// 84
// This will not work in TypeScript:
subtract(55, '15')
// TS error: Argument of type 'string' is not assignable to parameter of type 'number'.
// Use the type again:
const sumNumbers: TakeNumsReturnNums = (x, y) => x + y
// And again:
const divideNumbers: TakeNumsReturnNums = (c, d) => c / d
// Create interface for subtract function:
interface TakeNumsReturnNums {
// Syntax:
// (types for parameter list): return type;
(a: number, b: number): number;
}
// Use the "TakeNumsReturnNums" interface with function expression:
const subtract: TakeNumsReturnNums = (a, b) => a - b
// This will work in TypeScript:
subtract(55, 21)
// Output:
// 34
// This will not work in TypeScript:
subtract(true, 66)
// TS error: Argument of type 'boolean' is not assignable to parameter of type 'number'.
// Use the type again:
const sumNumbers: TakeNumsReturnNums = (x, y) => x + y
// And again:
const divideNumbers: TakeNumsReturnNums = (c, d) => c / d