Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/ext.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"disabled": [
"Codeium.codeium"
]
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This is my personal fork for learning proposes.

<a href="https://totaltypescript.com/tutorials/beginners-typescript"><img src="https://res.cloudinary.com/total-typescript/image/upload/v1709297838/github--beginngers-typescript_2x_p7vtmw.jpg" alt="beginner typescript tutorial" /></a>

## Quickstart
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
"express": "^4.18.1",
"zod": "^3.17.10"
}
}
}
21 changes: 0 additions & 21 deletions src/02-object-param.problem.ts

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, it } from "vitest";

export const addTwoNumbers = (a, b) => {
export const addTwoNumbers = (a: number, b: number) => {
return a + b;
};

Expand Down
32 changes: 32 additions & 0 deletions src/done/02-object-param.problem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect, it } from "vitest";

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;
};

it("Should add the two numbers together", () => {
expect(
addTwoNumbers({
first: 2,
second: 4,
})
).toEqual(6);

expect(
addTwoNumbers({
first: 10,
second: 20,
})
).toEqual(30);
});
Original file line number Diff line number Diff line change
@@ -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}`;
}
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions src/06-unions.problem.ts → src/done/06-unions.problem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
};
File renamed without changes.
17 changes: 16 additions & 1 deletion src/07-arrays.problem.ts → src/done/07-arrays.problem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Post>; // 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<Post>;

interface Post {
id: number;
title: string;
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,49 @@ interface User {
id: number;
firstName: string;
lastName: string;
role: "admin" | "user" | "super-admin";
role: Role;
posts: Array<Post>;
}

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();
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "@total-typescript/tsconfig/bundler",
"compilerOptions": {
"noUncheckedIndexedAccess": false,
"verbatimModuleSyntax": false
"verbatimModuleSyntax": false,
"strict": true
},
"include": [
"src"
Expand Down