This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Type Checking */ | |
"strict": true /* Enable all strict type-checking options. */, | |
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */ | |
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */ | |
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ | |
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */ | |
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ | |
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */ | |
// "useUnknownInCatchVariables": true, /* Type catch clause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"target": "ES6" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, | |
"lib": [ | |
"DOM", | |
"ES6", | |
"DOM.Iterable", | |
"ScriptHost" | |
] | |
/* Specify a set of bundled library declaration files that describe the target runtime environment. */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"compilerOptions": { | |
........ | |
........ | |
}, | |
"exclude": [ | |
"node_module", | |
// "analytics.ts" | |
], | |
"include": [ | |
"app.ts", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Utility function which build error function | |
function generateError(message: string, code: number): never { | |
throw { message, errorCode: code }; | |
} | |
generateError('An error occured', 500); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let userInput: unknown; | |
let userName: string; | |
userInput = 5; // OK | |
userInput = 'Max'; // OK | |
// userName = userInput; // KO – Type 'unknown' is not assignable to type 'string'. | |
if (typeof userInput === 'string') { | |
userName = userInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// callback | |
function addAndHandle(n1: number, n2: number, cb: (num: number) => void) { | |
const result = n1 + n2; | |
cb(result); | |
} | |
addAndHandle(1, 3, (result) => { | |
console.log(result); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// number return type | |
function addFunction(n1: number, n2: number): number { | |
return n1 + n2; | |
} | |
// void return type | |
function printResultFunction(num: number): void { | |
console.log('Result: ' + num); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// number return type | |
function add(n1: number, n2: number): number { | |
return n1 + n2; | |
} | |
// void return type | |
function printResult(num: number): void { | |
console.log('Result: ' + num); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Whatever = number; // it works but we should avoir it | |
type Combinable = number | string; // a new type we're creating based on a union type | |
type ConversionDescriptor = 'as-number' | 'as-text'; // a new type we're creating based on a literal type | |
function combineAliaces( | |
input1: Whatever | string, | |
input2: Combinable, | |
resultConversion: ConversionDescriptor, | |
) { | |
let result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function combine( | |
input1: number | string, | |
input2: number | string, | |
resultConversion: 'as-number' | 'as-text', // literal type | |
) { | |
let result; | |
if (typeof input1 === 'number' && typeof input2 === 'number') { | |
result = input1 + input2; | |
} else { | |
result = input1.toString() + input2.toString(); |