-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.ts
34 lines (23 loc) · 1.18 KB
/
functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function addNumbers(a:number, b:number): number {
return a + b ;
}
// module.exports disliked in ts
export default addNumbers;
export const addStrings = (str1: string, str2: string = ""): string => `${str1} ${str2}`;
// Union types
export const format = (title: string, param: string|number): string => `${title} ${param}`;
export const printFormat = (title: string, param: string | number): void => {
console.log(format(title, param));
}
// return promise
// change target to esnext to recognize promises in tsconfig
export const fetchData = (url: string): Promise<string> => Promise.resolve(`Data from ${url}`);
// multiple arguments with spread/rest
function introduce (salutation: string, ...names:string[] ):string {
return `${salutation} ${names.join( ' ')}`;
}
// types are enforced only at compile time not run time in TS
export function getName(user: {first: string; last: string}):string {
// return `${user?.first} ${user?.last}` //Optional chaining operator - returns undefined if value is not defined instead of error
return `${user?.first ?? 'first'} ${user?.last ?? 'last'}` //null coalescing operator- returns default with a check if it is not there
}