Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Finishing first lessons
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasOmar committed Nov 25, 2022
1 parent da3d8f1 commit ddaa51d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion 1-base-project/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
36 changes: 36 additions & 0 deletions 1-base-project/public/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
38 changes: 37 additions & 1 deletion 1-base-project/public/scripts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {}
/* 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)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down

0 comments on commit ddaa51d

Please sign in to comment.