-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a16308
commit 8a98a49
Showing
8 changed files
with
111 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Examples | ||
|
||
A curated list of awesome things related to Type Programming | ||
|
||
|
||
## Check Object base on string pattern | ||
|
||
Ref: https://type-level-typescript.com/ | ||
|
||
```ts twoslash | ||
// ✅ this is correct 👌 | ||
navigate("user/:userId", { userId: "2"}); | ||
|
||
// ✅ Looks good! `dashboardId` is optional. | ||
navigate("user/:userId/dashboard(/:dashboardId)", { userId: "2" }); | ||
|
||
// ❌ `userId` is missing. Add one to fix the error! | ||
// @ts-expect-error | ||
navigate("user/:userId/dashboard(/:dashboardId)", { dashboardId: "2" }); | ||
|
||
// ❌ `oops` isn't a parameter. Remove it to fix the error! | ||
// @ts-expect-error | ||
navigate("user/:userId/dashboard(/:dashboardId)", { userId: "2", oops: ":(" }); | ||
|
||
// 👇 Scroll to see how this works! | ||
|
||
// 🤫 Here are the kind of things you will soon be able to do! | ||
type ParseUrlParams<url> = | ||
url extends `${infer path}(${infer optionalPath})` | ||
? ParseUrlParams<path> & Partial<ParseUrlParams<optionalPath>> | ||
: url extends `${infer start}/${infer rest}` | ||
? ParseUrlParams<start> & ParseUrlParams<rest> | ||
: url extends `:${infer param}` | ||
? { [k in param]: string } | ||
: {}; | ||
|
||
// navigate to a different route | ||
function navigate<T extends string>( | ||
path: T, | ||
params: ParseUrlParams<T> | ||
) { | ||
// interpolate params | ||
let url = Object.entries<string>(params).reduce<string>( | ||
(path, [key, value]) => path.replace(`:${key}`, value), | ||
path | ||
); | ||
|
||
// clean url | ||
url = url.replace(/(\(|\)|\/?:[^\/]+)/g, '') | ||
|
||
// update url | ||
history.pushState({}, '', url); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Testing | ||
|
||
- https://www.npmjs.com/package/@type-challenges/utils | ||
- [Testing Types](https://vitest.dev/guide/testing-types) in Vitest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Type Programming | ||
|
||
Type programming or type-level programmming, definition from [Type-Level Programming](https://type-level-typescript.com/), some developer say about [the Turing Complete type system](https://github.com/microsoft/TypeScript/issues/14833) in Typescript. Whatever it is, in my book, I will describe as **Type Programming**. | ||
|
||
Type programming is the practice of writing programs that operate on types. In TypeScript, this is done using the type system. Type-level programming is a powerful technique that can be used to create complex type-level computations and to enforce constraints on types. | ||
|
||
## Here is curated list of awesome things related to Type Programming: | ||
|
||
- [type-challenges](https://github.com/type-challenges/type-challenges) Collection of TypeScript type challenges with online judge | ||
- [TypeScript Awesome Template Literal Types](https://github.com/ghoullier/awesome-template-literal-types) | ||
- [Type-Level Programming](https://type-level-typescript.com/) | ||
- [Extreme Explorations of TypeScript's Type System](https://www.learningtypescript.com/articles/extreme-explorations-of-typescripts-type-system) by Josh Goldberg | ||
- [TypeScript Type System Hacks](https://matt-rickard.com/typescript-type-system-hacks) by Matt Rickard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
# What's Type-safe? | ||
# What is Type-safe? | ||
|
||
Most community said that **type-safe** a lot, but no one try to define it. So, I will try to define it. | ||
|
||
Type-safe is a design pattern that provide a way to ensure that the data type is correct and consistent in the program. This pattern is very useful in modern programming language like TypeScript, where the type system is very powerful and can be used to ensure the correctness of the program. | ||
|
||
## The Traditional Type | ||
|
||
|
||
|
||
## Characteristics of Type-safe | ||
|
||
Many libraries and frameworks provide type-safe support, here is some characteristics of type-safe when the they claim that they are type-safe: | ||
|
||
- **Generic**: They use generic types | ||
- **Literal**: They use literal types | ||
- **Builder Pattern**: They use builder pattern | ||
- TBA | ||
- **Automatic Type Inference**: They use automatic type inference passing through the function and generic types |