Skip to content

Instantly share code, notes, and snippets.

View droidjahangir's full-sized avatar

jahangir alam droidjahangir

  • Dhaka,Bangladesh
View GitHub Profile
/* 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
"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. */
"compilerOptions": {
........
........
},
"exclude": [
"node_module",
// "analytics.ts"
],
"include": [
"app.ts",
// Utility function which build error function
function generateError(message: string, code: number): never {
throw { message, errorCode: code };
}
generateError('An error occured', 500);
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;
// callback
function addAndHandle(n1: number, n2: number, cb: (num: number) => void) {
const result = n1 + n2;
cb(result);
}
addAndHandle(1, 3, (result) => {
console.log(result);
});
// 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);
}
// 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);
}
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;
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();