Skip to content

Commit 93547e5

Browse files
committed
Intrinsic String Mapping
1 parent 461883d commit 93547e5

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

readme.md

+36-29
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ License MIT
8383
- [Recursive](#types-recursive)
8484
- [Conditional](#types-conditional)
8585
- [Template Literal](#types-template-literal)
86-
- [Intrinsic String](#types-intrinsic-string)
8786
- [Indexed](#types-indexed)
8887
- [Negated](#types-negated)
88+
- [Intrinsic](#types-intrinsic)
8989
- [Rest](#types-rest)
9090
- [Guards](#types-guards)
9191
- [Unsafe](#types-unsafe)
@@ -875,34 +875,6 @@ const R = Type.Record(T, Type.String()) // const R = {
875875
// }
876876
```
877877
878-
<a name='types-intrinsic-string'></a>
879-
880-
### Intrinsic String Types
881-
882-
TypeBox supports a set of intrinsic string mapping functions which can be used on string literals. These functions match the TypeScript string intrinsic types `Uppercase`, `Lowercase`, `Capitalize` and `Uncapitalize`. These functions are supported for literal strings, template literals and union types. The following shows the literal string usage.
883-
884-
```typescript
885-
// TypeScript
886-
887-
type A = Uncapitalize<'HELLO'> // type A = 'hELLO'
888-
889-
type B = Capitalize<'hello'> // type B = 'Hello'
890-
891-
type C = Uppercase<'hello'> // type C = 'HELLO'
892-
893-
type D = Lowercase<'HELLO'> // type D = 'hello'
894-
895-
// TypeBox
896-
897-
const A = Type.Uncapitalize(Type.Literal('HELLO')) // const A: TLiteral<'hELLO'>
898-
899-
const B = Type.Capitalize(Type.Literal('hello')) // const B: TLiteral<'Hello'>
900-
901-
const C = Type.Uppercase(Type.Literal('hello')) // const C: TLiteral<'HELLO'>
902-
903-
const D = Type.Lowercase(Type.Literal('HELLO')) // const D: TLiteral<'hello'>
904-
```
905-
906878
<a name='types-indexed'></a>
907879
908880
### Indexed Access Types
@@ -985,6 +957,41 @@ const Even = Type.Number({ multipleOf: 2 })
985957

986958
const Odd = Type.Intersect([Type.Number(), Type.Not(Even)])
987959
```
960+
961+
<a name='types-intrinsic'></a>
962+
963+
### Intrinsic String Types
964+
965+
TypeBox supports TypeScript intrinsic string manipulation types `Uppercase`, `Lowercase`, `Capitalize` and `Uncapitalize`. These can be applied to string literals, template literals and unions. The following shows general usage.
966+
967+
```typescript
968+
// TypeScript
969+
970+
type A = Capitalize<'hello'> // type A = 'Hello'
971+
972+
type B = Capitalize<'hello' | 'world'> // type C = 'Hello' | 'World'
973+
974+
type C = Capitalize<`hello${1|2|3}`> // type B = 'Hello1' | 'Hello2' | 'Hello3'
975+
976+
// TypeBox
977+
978+
const A = Type.Capitalize(Type.Literal('hello')) // const A: TLiteral<'Hello'>
979+
980+
const B = Type.Capitalize(Type.Union([ // const B: TUnion<[
981+
Type.Literal('hello'), // TLiteral<'Hello'>,
982+
Type.Literal('world') // TLiteral<'World'>
983+
])) // ]>
984+
985+
const C = Type.Capitalize( // const C: TTemplateLiteral<[
986+
Type.TemplateLiteral('hello${1|2|3}') // TLiteral<'Hello'>,
987+
) // TUnion<[
988+
// TLiteral<'1'>,
989+
// TLiteral<'2'>,
990+
// TLiteral<'3'>
991+
// ]>
992+
// ]>
993+
```
994+
988995
<a name='types-rest'></a>
989996
990997
### Rest Types

0 commit comments

Comments
 (0)