Skip to content

Commit

Permalink
doc: add type programming content
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed May 18, 2024
1 parent 5edf9e5 commit 9861927
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
4 changes: 3 additions & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ export default defineConfig({
collapsed: isCollapsed,
items: [
{ text: "Intro", link: "/type-programming/type-programming" },

{ text: "Conditional Types", link: "/type-programming/conditional-types" },
{ text: "Loop with Recursive Types" , link: "/type-programming/loop-with-recursive-types"},
{ text: "Template Literal Types", link: "/type-programming/template-literal-types" },
{ text: "Testing", link: "/type-programming/testing" },
{ text: "Examples", link: "/type-programming/examples" },
],
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ hero:
text: Starting Reading
link: /intro
- theme: alt
text: Design Patterns
link: /design-patterns/design-patterns
text: What is Type-safe?
link: /type-safe

features:
- title: 🎉   Next Level TypeScript
Expand Down
12 changes: 12 additions & 0 deletions docs/type-programming/conditional-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Conditional Types

In TypeScript, conditional types are a powerful feature that allows you to create types that depend on a condition. Conditional types are used to create types that change based on the properties of other types. They are often used in generic types to create more flexible and reusable code.

```ts twoslash
// Define a type that checks if a type is an array
type IsArray<T> = T extends any[] ? "yes" : "no";

// Test the IsArray type
type Test1 = IsArray<number>; // "no"
type Test2 = IsArray<string[]>; // "yes"
```
35 changes: 35 additions & 0 deletions docs/type-programming/loop-with-recursive-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Loop with Recursive Types

In this section, we will explore how to create a loop with recursive types in TypeScript. We will start by defining a simple recursive type and then show how to create a loop with it.

## Recursive Types

Basic recursive types are types that refer to themselves in their definition. For example, consider the following type definition:

```ts twoslash
type Recursive<T> = T extends [infer Head, ...infer Tail]
? [Head, ...Recursive<Tail>]
: [];
```

In this definition, the `Recursive` type is defined in terms of itself. It takes a type `T` and checks if `T` extends an array with a head element and a tail element. If it does, it constructs a new array type by recursively applying `Recursive` to the tail element.

## Examples

```ts twoslash
// This case Tail always be array
type CheckTailStatus<T extends any[]> = T extends [infer H, ...infer T]
? T extends any[]
? 'Tail is array'
: 'Tail is not array'
: 'empty array';

// The result will be "empty array"
type check1 = CheckTailStatus<[]>;
// The result will be "Tail is array"
type check2 = CheckTailStatus<['hello']>;
// The result will be "Tail is array"
type check3 = CheckTailStatus<['hello', 'wolrd']>;
```

In this example, we define a type `CheckTailStatus` that checks if the tail of an array is an array or not. We use conditional types to check if the tail element `T` extends an array. If it does, we return `'Tail is array'`; otherwise, we return `'Tail is not array'`.
6 changes: 6 additions & 0 deletions docs/type-programming/template-literal-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Template Literal Types

In TypeScript, template literal types are a powerful feature that allows you to create types by combining string literals and expressions. Template literal types are used to create types that depend on the values of other types. They are often used in generic types to create more flexible and reusable code.


Checkout [TypeScript Awesome Template Literal Types](https://github.com/ghoullier/awesome-template-literal-types) for more resources.
2 changes: 1 addition & 1 deletion docs/type-safe.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Most community said that **type-safe** a lot, but no one try to define it. So, I
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

TBA


## Characteristics of Type-safe
Expand Down

0 comments on commit 9861927

Please sign in to comment.