From ddaa51d15d368e3a3bb676f2359cd2d05c031ee7 Mon Sep 17 00:00:00 2001 From: NicolasOmar Date: Fri, 25 Nov 2022 14:37:35 -0300 Subject: [PATCH] Finishing first lessons --- 1-base-project/package.json | 2 +- 1-base-project/public/scripts/app.js | 36 ++++++++++++++++++++++++++ 1-base-project/public/scripts/app.ts | 38 +++++++++++++++++++++++++++- README.md | 2 +- 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/1-base-project/package.json b/1-base-project/package.json index e0c2e41..2e5d217 100644 --- a/1-base-project/package.json +++ b/1-base-project/package.json @@ -1,6 +1,6 @@ { "name": "1-base-project", - "version": "0.0.1", + "version": "0.1.0", "private": true, "scripts": { "start": "lite-server -c configs/server.json", diff --git a/1-base-project/public/scripts/app.js b/1-base-project/public/scripts/app.js index 1790464..8896884 100644 --- a/1-base-project/public/scripts/app.js +++ b/1-base-project/public/scripts/app.js @@ -36,3 +36,39 @@ console.info('VOID TYPE', printResult(testAddWithType(1, 3)), printResult(testAd // If you try to return a undefined type value, you must indicate a return statement without anything else // BUT, is preffered that your functions return a void value instead to avoid writing the empty return statement var returnUndefined = function () { return; }; +console.info('UNDEFINED TYPE', returnUndefined(), returnUndefined(), returnUndefined()); +// FUNCTION TYPE is used to return another function +var returnFunction; +// In the following cases, you return an empty function and a functions without been executed +returnFunction = function () { }; +returnFunction = function () { }; +returnFunction = returnUndefined; +// Here the function needs a specific type of function which will recieve two numbers and return another number +var returnNumberFunction; +var addNumbers = function (n1, n2) { return (n1 + n2); }; +returnNumberFunction = addNumbers; +// On this case, you using a function callback as a parameter wich will recieve a result and handle it without care about what is going to return +var addAndHandle = function (n1, n2, cb) { return cb(n1 + n2); }; +addAndHandle(3, 5, printResult); +// UNKNOWN TYPE, we dont know yet what the user is going to input +var userInput; +var userName; +userInput = 5; +userInput = '5'; +userInput = null; +/* Unknown type is used to check other variables types before assign its value. + * If a string variable tries to be assigned to a unknown one, it will break the compilation + * unless you check its typeof before + * Is a better alternative than use any (for previous type checking) +*/ +if (typeof userInput === 'string') { + userName = userInput; +} +/* VOID/NEVER TYPE is used when you want a function does not retun any value + * It can be used for special cases as error handling, because it does not return + * a value, it just throws an error +*/ +var generateError = function (message, code) { + throw { message: message, errorCode: code }; +}; +generateError('An error ocurred!', 500); diff --git a/1-base-project/public/scripts/app.ts b/1-base-project/public/scripts/app.ts index abae933..48288ec 100644 --- a/1-base-project/public/scripts/app.ts +++ b/1-base-project/public/scripts/app.ts @@ -79,5 +79,41 @@ console.info( // FUNCTION TYPE is used to return another function let returnFunction: Function +// In the following cases, you return an empty function and a functions without been executed +returnFunction = () => {} +returnFunction = function () {} +returnFunction = returnUndefined + +// Here the function needs a specific type of function which will recieve two numbers and return another number +let returnNumberFunction: (a: number, b: number) => number +const addNumbers = (n1: number, n2: number) => (n1 + n2) +returnNumberFunction = addNumbers + +// On this case, you using a function callback as a parameter wich will recieve a result and handle it without care about what is going to return +let addAndHandle = (n1: number, n2: number, cb: (n3: number) => void) => cb(n1 + n2) +addAndHandle(3, 5, printResult) + +// UNKNOWN TYPE, we dont know yet what the user is going to input +let userInput: unknown +let userName: string + +userInput = 5 +userInput = '5' +userInput = null +/* Unknown type is used to check other variables types before assign its value. + * If a string variable tries to be assigned to a unknown one, it will break the compilation + * unless you check its typeof before + * Is a better alternative than use any (for previous type checking) +*/ +if (typeof userInput === 'string') { + userName = userInput +} -returnFunction = () => {} \ No newline at end of file +/* VOID/NEVER TYPE is used when you want a function does not retun any value + * It can be used for special cases as error handling, because it does not return + * a value, it just throws an error +*/ +const generateError = (message: string, code: number): void | never => { + throw { message, errorCode: code } +} +generateError('An error ocurred!', 500) \ No newline at end of file diff --git a/README.md b/README.md index 281cd23..245b927 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ npm start - Create, code and compile a `.ts` file into javascript - Understanding of `Core Types` like `number`, `string`, `boolean`, `array` and `object` - Understanding of `Tuples`, `Enums`, `Union` and `Literal/Custom` Types - - Understanding of `void` and `undefined` Types + - Understanding of `void`, `undefined`, `unknown` and `never` Types ## Version (currently ![Typescript practice version](https://img.shields.io/github/package-json/v/nicolasomar/typescript-practice?color=success&label=%20&style=flat-square)) | Number | Meaning |