Skip to content

Commit

Permalink
2023-09-19T11:01:48.172Z
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Sep 19, 2023
1 parent a0c3d63 commit aa818a2
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Modelling your domain in TypeScript

// Think of the entities in your domain as individual types

// Compose those types together

// Think about types as containers for other types
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type PromiseFunc = (input: any) => Promise<any>;

type Example1 = PromiseFunc<string, string>;

type test1 = Expect<Equal<Example1, (input: string) => Promise<string>>>;

type Example2 = PromiseFunc<boolean, number>;

type test2 = Expect<Equal<Example2, (input: boolean) => Promise<number>>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type PromiseFunc<Input, Output> = (input: Input) => Promise<Output>;

type Example1 = PromiseFunc<string, string>;

type test1 = Expect<Equal<Example1, (input: string) => Promise<string>>>;

type Example2 = PromiseFunc<boolean, number>;

type test2 = Expect<Equal<Example2, (input: boolean) => Promise<number>>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type Result<TResult, TError = Error> =
| {
success: true;
data: TResult;
}
| {
success: false;
error: TError;
};

type BadExample = Result<
{ id: string },
// @ts-expect-error Should be an object with a message property
string
>;

type GoodExample = Result<{ id: string }, TypeError>;
type GoodExample2 = Result<{ id: string }, { message: string; code: number }>;
type GoodExample3 = Result<{ id: string }, { message: string }>;
type GoodExample4 = Result<{ id: string }>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type Result<TResult, TError extends { message: string } = Error> =
| {
success: true;
data: TResult;
}
| {
success: false;
error: TError;
};

type BadExample = Result<
{ id: string },
// @ts-expect-error Should be an object with a message property
string
>;

type GoodExample = Result<{ id: string }, TypeError>;
type GoodExample2 = Result<{ id: string }, { message: string; code: number }>;
type GoodExample3 = Result<{ id: string }, { message: string }>;
type GoodExample4 = Result<{ id: string }>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type StrictOmit<T, K> = Omit<T, K>;

type ShouldFail = StrictOmit<
{ a: string },
// @ts-expect-error
"b"
>;

type tests = [
Expect<Equal<StrictOmit<{ a: string; b: number }, "b">, { a: string }>>,
Expect<Equal<StrictOmit<{ a: string; b: number }, "b" | "a">, {}>>,
Expect<
Equal<StrictOmit<{ a: string; b: number }, never>, { a: string; b: number }>
>,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type StrictOmit<T, K extends keyof T> = Omit<T, K>;

type ShouldFail = StrictOmit<
{ a: string },
// @ts-expect-error
"b"
>;

type tests = [
Expect<Equal<StrictOmit<{ a: string; b: number }, "b">, { a: string }>>,
Expect<Equal<StrictOmit<{ a: string; b: number }, "b" | "a">, {}>>,
Expect<
Equal<StrictOmit<{ a: string; b: number }, never>, { a: string; b: number }>
>,
];

0 comments on commit aa818a2

Please sign in to comment.