-
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
5edf9e5
commit 9861927
Showing
6 changed files
with
59 additions
and
4 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
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" | ||
``` |
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,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'`. |
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,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. |
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