-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(WithPrefix): Add
WithPrefix
(#38)
* feat(WithPrefix): Add WithPrefix type * test(WithPrefix): Add WithPrefix type test * docs(WithPrefix): Add WithPrefix type documentation
- Loading branch information
1 parent
d7f5419
commit d77873f
Showing
7 changed files
with
97 additions
and
0 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,28 @@ | ||
# WithPrefix\<BaseString, Prefix>; | ||
|
||
## 개요 | ||
|
||
`WithPrefix` 타입은 기존 문자열 타입 앞에 지정된 접두사를 추가하여 새로운 문자열 타입을 생성해요. 이를 통해 문자열 타입에 일관된 접두사를 자동으로 추가할 수 있어요. | ||
|
||
## 문법 | ||
|
||
```ts | ||
type WithPrefix< | ||
BaseString extends string, | ||
Prefix extends string, | ||
> = `${Prefix}${BaseString}`; | ||
``` | ||
|
||
- **BaseString**: 접두사를 추가할 기존 문자열 타입이에요. | ||
- **Prefix**: 추가될 접두사예요. | ||
|
||
## 예제 | ||
|
||
#### 예제 #1 | ||
|
||
```ts | ||
type FormEventNames = 'submit' | 'reset' | 'change'; | ||
type CapitalizedFormEventNames = Capitalize<FormEventNames>; | ||
type FormEventHandlers = WithPrefix<CapitalizedFormEventNames, 'on'>; | ||
// Result: "onSubmit" | "onReset" | "onChange"; | ||
``` |
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,28 @@ | ||
# WithPrefix\<BaseString, Prefix>; | ||
|
||
## Overview | ||
|
||
The `WithPrefix` type creates a new string type by prepending a specified prefix to an existing string type. This enables automatic addition of a consistent prefix to string types. | ||
|
||
## Syntax | ||
|
||
```ts | ||
type WithPrefix< | ||
BaseString extends string, | ||
Prefix extends string, | ||
> = `${Prefix}${BaseString}`; | ||
``` | ||
|
||
- **BaseString**: The base string type to which the prefix will be added. | ||
- **Prefix**: The prefix to be added. | ||
|
||
## Examples | ||
|
||
#### Example #1 | ||
|
||
```ts | ||
type FormEventNames = 'submit' | 'reset' | 'change'; | ||
type CapitalizedFormEventNames = Capitalize<FormEventNames>; | ||
type FormEventHandlers = WithPrefix<CapitalizedFormEventNames, 'on'>; | ||
// Result: "onSubmit" | "onReset" | "onChange"; | ||
``` |
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,22 @@ | ||
/** | ||
* @description | ||
* The `WithPrefix` type creates a new string type by prepending a specified prefix to an existing | ||
* string type. This enables automatic addition of a consistent prefix to string types. | ||
* | ||
* @template {string} BaseString - The base string type to which the prefix will be added. | ||
* @template {string} Prefix - The prefix to be added. | ||
* | ||
* @returns | ||
* A new string type with the specified prefix prepended. | ||
* | ||
* @example | ||
* type FormEventNames = "submit" | "reset" | "change"; | ||
* type CapitalizedFormEventNames = Capitalize<FormEventNames>; | ||
* type FormHandlerNames = WithPrefix<CapitalizedFormEventNames, "on"> | ||
* // Result: "onSubmit" | "onReset" | "onChange" | ||
*/ | ||
|
||
export type WithPrefix< | ||
BaseString extends string, | ||
Prefix extends string, | ||
> = `${Prefix}${BaseString}`; |
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,3 +1,4 @@ | ||
export type { Replace } from './Replace'; | ||
export type { ReplaceAll } from './ReplaceAll'; | ||
export type { Split } from './Split'; | ||
export type { WithPrefix } from './WithPrefix'; |
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,16 @@ | ||
import { WithPrefix } from '@/string'; | ||
import { expectType } from 'tsd'; | ||
|
||
declare function withPrefix< | ||
S extends string, | ||
Prefix extends string, | ||
>(): WithPrefix<S, Prefix>; | ||
|
||
expectType<'onSubmit'>(withPrefix<'Submit', 'on'>()); | ||
|
||
type FormEventNames = 'submit' | 'reset' | 'change'; | ||
type CapitalizedFormEventNames = Capitalize<FormEventNames>; | ||
|
||
expectType<'onSubmit' | 'onReset' | 'onChange'>( | ||
withPrefix<CapitalizedFormEventNames, 'on'>() | ||
); |