-
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
Showing
22 changed files
with
219 additions
and
49 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
2 changes: 1 addition & 1 deletion
2
docs/data-structure/data-structure.md → docs/basic-types/basic-types.md
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,70 @@ | ||
# Generics | ||
|
||
## Basic Generics | ||
|
||
TBA... | ||
|
||
## Understanding Type Inference from Function Arguments | ||
|
||
When you pass a function as an argument, TypeScript will infer the type of the function based on the arguments passed to it. This is known as type inference. | ||
|
||
```ts twoslash | ||
function identity<T>(arg: T): T { | ||
return arg; | ||
} | ||
|
||
identity(42); // number | ||
// ^? | ||
|
||
// End of Example | ||
``` | ||
|
||
In the above example, TypeScript infers the type of `T` as `number` because the argument passed to the `identity` function is a number. | ||
|
||
### Think Generic Param as a variable | ||
|
||
Generics are like variables for types. When you define a generic type, you are defining a placeholder for a type that will be determined when the generic type is used. | ||
|
||
Defining generic types is similar to defining variables, you can use in every place where it is in scope. For example below, `Path` is a generic type that can be used in the function signature and return type. | ||
|
||
```ts twoslash | ||
type Paths = { | ||
"/": "home"; | ||
"/about": "about"; | ||
"/users": "users"; | ||
}; | ||
|
||
function routeTo<Path extends keyof Paths>(path: Path) { | ||
return path as Path; | ||
} | ||
|
||
routeTo("/users"); | ||
//^? | ||
|
||
// End of Example | ||
``` | ||
|
||
Moreover, you can use generic with any type features such as template literal types. | ||
If I want to function that recieve one more argument and return a string that is a combination of the two arguments, I can use template literal types with generics. | ||
|
||
```ts twoslash | ||
type Paths = { | ||
"/": "home"; | ||
"/about": "about"; | ||
"/users": "users"; | ||
}; | ||
|
||
function routeTo< | ||
Path extends keyof Paths, | ||
Param extends string | ||
>(path: Path, params: Param) { | ||
return `${path}/${params}` as `${Path}/${Param}`; | ||
} | ||
|
||
routeTo("/users", '324'); | ||
// ^? | ||
|
||
// End of Example | ||
``` | ||
|
||
For example, the `routeTo` function takes two arguments: `path` and `params`. The `path` argument is a key of the `Paths` type, and the `params` argument is a string. The function returns a string that is a combination of the two arguments. So, the return type will be `"/users/324"`. |
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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 @@ | ||
# Design Guideline |
File renamed without changes.
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,3 @@ | ||
# Type-safe Level | ||
|
||
Type-safe level is a measure of how much a project is type-safe. It is a subjective measure and can be different from one person to another. However, it is important to have a common understanding of what it means to be type-safe. |
File renamed without changes.
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,8 @@ | ||
{ | ||
"trailingSlash": "never", | ||
"responseOverrides": { | ||
"404": { | ||
"rewrite": "/404.html" | ||
} | ||
} | ||
} |
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
Oops, something went wrong.