Skip to content

Commit

Permalink
2023-09-13T15:25:30.037Z
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Sep 13, 2023
1 parent 778199c commit a2a192b
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/075-designing-your-types/179-intro-to-helper-types.problem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
type ErrorShape = {
error: {
message: string;
};
};

type UserDataShape =
| {
data: {
id: string;
name: string;
email: string;
};
}
| ErrorShape;

type PostDataShape =
| {
data: {
id: string;
title: string;
body: string;
};
}
| ErrorShape;

type tests = [
Expect<
Equal<
UserDataShape,
| {
data: {
id: string;
name: string;
email: string;
};
}
| {
error: {
message: string;
};
}
>
>,
Expect<
Equal<
PostDataShape,
| {
data: {
id: string;
title: string;
body: string;
};
}
| {
error: {
message: string;
};
}
>
>,
];
60 changes: 60 additions & 0 deletions src/075-designing-your-types/179-intro-to-helper-types.solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
type ErrorShape = {
error: {
message: string;
};
};

type DataShape<T> =
| {
data: T;
}
| ErrorShape;

type UserDataShape = DataShape<{
id: string;
name: string;
email: string;
}>;

type PostDataShape = DataShape<{
id: string;
title: string;
body: string;
}>;

type tests = [
Expect<
Equal<
UserDataShape,
| {
data: {
id: string;
name: string;
email: string;
};
}
| {
error: {
message: string;
};
}
>
>,
Expect<
Equal<
PostDataShape,
| {
data: {
id: string;
title: string;
body: string;
};
}
| {
error: {
message: string;
};
}
>
>,
];
Empty file.
Empty file.
37 changes: 37 additions & 0 deletions src/075-designing-your-types/181-result-type.explainer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
type Result<TResult, TError> =
| {
success: true;
data: TResult;
}
| {
success: false;
error: TError;
};

const createRandomNumber = (): Result<number, Error> => {
const num = Math.random();

if (num > 0.5) {
return {
success: true,
data: 123,
};
}

return {
success: false,
error: new Error("Something went wrong"),
};
};

const result = createRandomNumber();

if (result.success) {
console.log(result.data);

type test = Expect<Equal<typeof result.data, number>>;
} else {
console.error(result.error);

type test = Expect<Equal<typeof result.error, Error>>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
type Result<TResult, TError> =
| {
success: true;
data: TResult;
}
| {
success: false;
error: TError;
};

const createRandomNumber = (): Result<number> => {
const num = Math.random();

if (num > 0.5) {
return {
success: true,
data: 123,
};
}

return {
success: false,
error: new Error("Something went wrong"),
};
};

const result = createRandomNumber();

if (result.success) {
console.log(result.data);

type test = Expect<Equal<typeof result.data, number>>;
} else {
console.error(result.error);

type test = Expect<Equal<typeof result.error, Error>>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
type Result<TResult, TError = Error> =
| {
success: true;
data: TResult;
}
| {
success: false;
error: TError;
};

const createRandomNumber = (): Result<number> => {
const num = Math.random();

if (num > 0.5) {
return {
success: true,
data: 123,
};
}

return {
success: false,
error: new Error("Something went wrong"),
};
};

const result = createRandomNumber();

if (result.success) {
console.log(result.data);

type test = Expect<Equal<typeof result.data, number>>;
} else {
console.error(result.error);

type test = Expect<Equal<typeof result.error, Error>>;
}

0 comments on commit a2a192b

Please sign in to comment.