From bea53ede3f840cb71be5bb300394fa5ba01eaaf7 Mon Sep 17 00:00:00 2001 From: Joaquin Segovia Light-it Date: Wed, 12 Feb 2025 21:07:24 -0300 Subject: [PATCH 1/4] chore: test remote collaborator --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 17d76d19..45c333c3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +# This is my personal fork for learning proposes. + beginner typescript tutorial ## Quickstart From 5f7ac55ce6b836ba14e4b44184ec30b7c2260402 Mon Sep 17 00:00:00 2001 From: Joaquin Segovia Light-it Date: Wed, 12 Feb 2025 22:28:18 -0300 Subject: [PATCH 2/4] feat: first and second exercise --- .vscode/ext.config.json | 5 +++++ package.json | 2 +- src/01-number.problem.ts | 2 +- src/02-object-param.problem.ts | 17 ++++++++++++++--- tsconfig.json | 3 ++- 5 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 .vscode/ext.config.json diff --git a/.vscode/ext.config.json b/.vscode/ext.config.json new file mode 100644 index 00000000..e1c35747 --- /dev/null +++ b/.vscode/ext.config.json @@ -0,0 +1,5 @@ +{ + "disabled": [ + "Codeium.codeium" + ] +} \ No newline at end of file diff --git a/package.json b/package.json index 1263eee7..af251048 100644 --- a/package.json +++ b/package.json @@ -63,4 +63,4 @@ "express": "^4.18.1", "zod": "^3.17.10" } -} +} \ No newline at end of file diff --git a/src/01-number.problem.ts b/src/01-number.problem.ts index 0f6286e0..5f21a88e 100644 --- a/src/01-number.problem.ts +++ b/src/01-number.problem.ts @@ -1,6 +1,6 @@ import { expect, it } from "vitest"; -export const addTwoNumbers = (a, b) => { +export const addTwoNumbers = (a: number, b: number) => { return a + b; }; diff --git a/src/02-object-param.problem.ts b/src/02-object-param.problem.ts index 8c331765..9a11faf7 100644 --- a/src/02-object-param.problem.ts +++ b/src/02-object-param.problem.ts @@ -1,6 +1,17 @@ import { expect, it } from "vitest"; -export const addTwoNumbers = (params) => { +interface AddTwoNumbersArgs { + first: number; + second: number; +} + +// type AddTwoNumbersArgs = { +// first: number; +// second: number; +// }; + +export const addTwoNumbers = (params: AddTwoNumbersArgs) => { + // export const addTwoNumbers = (params: { first: number; second: number }) => { return params.first + params.second; }; @@ -9,13 +20,13 @@ it("Should add the two numbers together", () => { addTwoNumbers({ first: 2, second: 4, - }), + }) ).toEqual(6); expect( addTwoNumbers({ first: 10, second: 20, - }), + }) ).toEqual(30); }); diff --git a/tsconfig.json b/tsconfig.json index 2ca5a708..f17af1f3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,8 @@ "extends": "@total-typescript/tsconfig/bundler", "compilerOptions": { "noUncheckedIndexedAccess": false, - "verbatimModuleSyntax": false + "verbatimModuleSyntax": false, + "strict": true }, "include": [ "src" From 0a60079454be0f330dc686ea78c76352b963b8a6 Mon Sep 17 00:00:00 2001 From: Joaquin Segovia Light-it Date: Mon, 17 Feb 2025 22:20:30 -0300 Subject: [PATCH 3/4] feat: exercises from 3 to 6 --- src/03-optional-properties.problem.ts | 4 +++- src/04-optional-params.problem.ts | 2 +- src/05-assigning-types-to-variables.problem.ts | 7 ++++++- src/06-unions.problem.ts | 12 +++++++++--- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/03-optional-properties.problem.ts b/src/03-optional-properties.problem.ts index 9ee58fcb..83fc3653 100644 --- a/src/03-optional-properties.problem.ts +++ b/src/03-optional-properties.problem.ts @@ -1,6 +1,7 @@ import { expect, it } from "vitest"; -export const getName = (params: { first: string; last: string }) => { +// export const getName = (params: { first: string; last: string | undefined }) => { +export const getName = (params: { first: string; last?: string }) => { if (params.last) { return `${params.first} ${params.last}`; } @@ -10,6 +11,7 @@ export const getName = (params: { first: string; last: string }) => { it("Should work with just the first name", () => { const name = getName({ first: "Matt", + // last: undefined, }); expect(name).toEqual("Matt"); diff --git a/src/04-optional-params.problem.ts b/src/04-optional-params.problem.ts index 023bb997..3c12b9f8 100644 --- a/src/04-optional-params.problem.ts +++ b/src/04-optional-params.problem.ts @@ -1,6 +1,6 @@ import { expect, it } from "vitest"; -export const getName = (first: string, last: string) => { +export const getName = (first: string, last?: string) => { if (last) { return `${first} ${last}`; } diff --git a/src/05-assigning-types-to-variables.problem.ts b/src/05-assigning-types-to-variables.problem.ts index 50de8989..174c0fc0 100644 --- a/src/05-assigning-types-to-variables.problem.ts +++ b/src/05-assigning-types-to-variables.problem.ts @@ -11,7 +11,12 @@ interface User { * How do we ensure that defaultUser is of type User * at THIS LINE - not further down in the code? */ -const defaultUser = {}; +const defaultUser: User = { + id: 1, + firstName: "Matt", + lastName: "Pocock", + isAdmin: true, +}; const getUserId = (user: User) => { return user.id; diff --git a/src/06-unions.problem.ts b/src/06-unions.problem.ts index 55420fd0..f11cbf25 100644 --- a/src/06-unions.problem.ts +++ b/src/06-unions.problem.ts @@ -8,13 +8,19 @@ interface User { * - 'user' * - 'super-admin' */ - role: string; + // this is a union type:: + role: Role; } +type Role = "admin" | "user" | SuperAdmin | { wow: boolean }; + +type SuperAdmin = "super-admin"; export const defaultUser: User = { id: 1, firstName: "Matt", lastName: "Pocock", - // @ts-expect-error - role: "I_SHOULD_NOT_BE_ALLOWED", + // // @ts-expect-error + // role: "I_SHOULD_NOT_BE_ALLOWED", + role: "super-admin", + // role: { wow: true }, }; From e13d9365670377d0b419262ed11b5de04b949c94 Mon Sep 17 00:00:00 2001 From: Joaquin Segovia Light-it Date: Tue, 18 Feb 2025 22:38:28 -0300 Subject: [PATCH 4/4] feat: exercise 7 and 8 --- src/{ => done}/01-number.problem.ts | 0 src/{ => done}/01-number.solution.ts | 0 src/{ => done}/02-object-param.problem.ts | 0 src/{ => done}/02-object-param.solution.1.ts | 0 src/{ => done}/02-object-param.solution.2.ts | 0 src/{ => done}/02-object-param.solution.3.ts | 0 .../03-optional-properties.problem.ts | 0 .../03-optional-properties.solution.ts | 0 src/{ => done}/04-optional-params.problem.ts | 0 src/{ => done}/04-optional-params.solution.ts | 0 ...05-assigning-types-to-variables.problem.ts | 0 ...5-assigning-types-to-variables.solution.ts | 0 src/{ => done}/06-unions.problem.ts | 0 src/{ => done}/06-unions.solution.ts | 0 src/{ => done}/07-arrays.problem.ts | 17 ++++++++- src/{ => done}/07-arrays.solution.1.ts | 0 src/{ => done}/07-arrays.solution.2.ts | 0 ...unction-return-type-annotations.problem.ts | 35 ++++++++++++++++--- ...nction-return-type-annotations.solution.ts | 0 19 files changed, 47 insertions(+), 5 deletions(-) rename src/{ => done}/01-number.problem.ts (100%) rename src/{ => done}/01-number.solution.ts (100%) rename src/{ => done}/02-object-param.problem.ts (100%) rename src/{ => done}/02-object-param.solution.1.ts (100%) rename src/{ => done}/02-object-param.solution.2.ts (100%) rename src/{ => done}/02-object-param.solution.3.ts (100%) rename src/{ => done}/03-optional-properties.problem.ts (100%) rename src/{ => done}/03-optional-properties.solution.ts (100%) rename src/{ => done}/04-optional-params.problem.ts (100%) rename src/{ => done}/04-optional-params.solution.ts (100%) rename src/{ => done}/05-assigning-types-to-variables.problem.ts (100%) rename src/{ => done}/05-assigning-types-to-variables.solution.ts (100%) rename src/{ => done}/06-unions.problem.ts (100%) rename src/{ => done}/06-unions.solution.ts (100%) rename src/{ => done}/07-arrays.problem.ts (60%) rename src/{ => done}/07-arrays.solution.1.ts (100%) rename src/{ => done}/07-arrays.solution.2.ts (100%) rename src/{ => done}/08-function-return-type-annotations.problem.ts (53%) rename src/{ => done}/08-function-return-type-annotations.solution.ts (100%) diff --git a/src/01-number.problem.ts b/src/done/01-number.problem.ts similarity index 100% rename from src/01-number.problem.ts rename to src/done/01-number.problem.ts diff --git a/src/01-number.solution.ts b/src/done/01-number.solution.ts similarity index 100% rename from src/01-number.solution.ts rename to src/done/01-number.solution.ts diff --git a/src/02-object-param.problem.ts b/src/done/02-object-param.problem.ts similarity index 100% rename from src/02-object-param.problem.ts rename to src/done/02-object-param.problem.ts diff --git a/src/02-object-param.solution.1.ts b/src/done/02-object-param.solution.1.ts similarity index 100% rename from src/02-object-param.solution.1.ts rename to src/done/02-object-param.solution.1.ts diff --git a/src/02-object-param.solution.2.ts b/src/done/02-object-param.solution.2.ts similarity index 100% rename from src/02-object-param.solution.2.ts rename to src/done/02-object-param.solution.2.ts diff --git a/src/02-object-param.solution.3.ts b/src/done/02-object-param.solution.3.ts similarity index 100% rename from src/02-object-param.solution.3.ts rename to src/done/02-object-param.solution.3.ts diff --git a/src/03-optional-properties.problem.ts b/src/done/03-optional-properties.problem.ts similarity index 100% rename from src/03-optional-properties.problem.ts rename to src/done/03-optional-properties.problem.ts diff --git a/src/03-optional-properties.solution.ts b/src/done/03-optional-properties.solution.ts similarity index 100% rename from src/03-optional-properties.solution.ts rename to src/done/03-optional-properties.solution.ts diff --git a/src/04-optional-params.problem.ts b/src/done/04-optional-params.problem.ts similarity index 100% rename from src/04-optional-params.problem.ts rename to src/done/04-optional-params.problem.ts diff --git a/src/04-optional-params.solution.ts b/src/done/04-optional-params.solution.ts similarity index 100% rename from src/04-optional-params.solution.ts rename to src/done/04-optional-params.solution.ts diff --git a/src/05-assigning-types-to-variables.problem.ts b/src/done/05-assigning-types-to-variables.problem.ts similarity index 100% rename from src/05-assigning-types-to-variables.problem.ts rename to src/done/05-assigning-types-to-variables.problem.ts diff --git a/src/05-assigning-types-to-variables.solution.ts b/src/done/05-assigning-types-to-variables.solution.ts similarity index 100% rename from src/05-assigning-types-to-variables.solution.ts rename to src/done/05-assigning-types-to-variables.solution.ts diff --git a/src/06-unions.problem.ts b/src/done/06-unions.problem.ts similarity index 100% rename from src/06-unions.problem.ts rename to src/done/06-unions.problem.ts diff --git a/src/06-unions.solution.ts b/src/done/06-unions.solution.ts similarity index 100% rename from src/06-unions.solution.ts rename to src/done/06-unions.solution.ts diff --git a/src/07-arrays.problem.ts b/src/done/07-arrays.problem.ts similarity index 60% rename from src/07-arrays.problem.ts rename to src/done/07-arrays.problem.ts index c18909ce..d79b466b 100644 --- a/src/07-arrays.problem.ts +++ b/src/done/07-arrays.problem.ts @@ -3,9 +3,24 @@ interface User { firstName: string; lastName: string; role: "admin" | "user" | "super-admin"; - posts: Post; + // posts: Post[]; // option 1 + // posts: Posts; // option 2 + posts: Array; // option 2 } +const myPostOption1: Post[] = [ + { + id: 1, + title: "How I eat so much cheese", + }, + { + id: 2, + title: "Why I don't eat more vegetables", + }, +]; + +// type Posts = Array; + interface Post { id: number; title: string; diff --git a/src/07-arrays.solution.1.ts b/src/done/07-arrays.solution.1.ts similarity index 100% rename from src/07-arrays.solution.1.ts rename to src/done/07-arrays.solution.1.ts diff --git a/src/07-arrays.solution.2.ts b/src/done/07-arrays.solution.2.ts similarity index 100% rename from src/07-arrays.solution.2.ts rename to src/done/07-arrays.solution.2.ts diff --git a/src/08-function-return-type-annotations.problem.ts b/src/done/08-function-return-type-annotations.problem.ts similarity index 53% rename from src/08-function-return-type-annotations.problem.ts rename to src/done/08-function-return-type-annotations.problem.ts index af1e7217..a856f348 100644 --- a/src/08-function-return-type-annotations.problem.ts +++ b/src/done/08-function-return-type-annotations.problem.ts @@ -4,22 +4,49 @@ interface User { id: number; firstName: string; lastName: string; - role: "admin" | "user" | "super-admin"; + role: Role; posts: Array; } +type Role = "admin" | "user" | "super-admin"; + interface Post { id: number; title: string; } +const userToReturn: User = { + id: 1, + firstName: "Matt", + lastName: "Pocock", + role: "user", + posts: [ + { + id: 1, + title: "How I eat so much cheese", + }, + ], +}; + /** * How do we ensure that makeUser ALWAYS * returns a user? */ -const makeUser = () => { - return {}; -}; +const makeUser = (): User => userToReturn; +// const makeUser = (): User => { +// return { +// id: 1, +// firstName: "Matt", +// lastName: "Pocock", +// role: "super-admin", +// posts: [ +// { +// id: 1, +// title: "How I eat so much cheese", +// }, +// ], +// }; +// }; it("Should return a valid user", () => { const user = makeUser(); diff --git a/src/08-function-return-type-annotations.solution.ts b/src/done/08-function-return-type-annotations.solution.ts similarity index 100% rename from src/08-function-return-type-annotations.solution.ts rename to src/done/08-function-return-type-annotations.solution.ts