Decompose monolithic readme into docs/ directory structure
This commit is contained in:
76
docs/usage.md
Normal file
76
docs/usage.md
Normal file
@@ -0,0 +1,76 @@
|
||||
# Usage
|
||||
|
||||
The following shows general usage.
|
||||
|
||||
```typescript
|
||||
import { Type, type Static } from "@alkdev/typebox";
|
||||
|
||||
//--------------------------------------------------------------------------------------------
|
||||
//
|
||||
// Let's say you have the following type ...
|
||||
//
|
||||
//--------------------------------------------------------------------------------------------
|
||||
|
||||
type T = {
|
||||
id: string;
|
||||
name: string;
|
||||
timestamp: number;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------------------------
|
||||
//
|
||||
// ... you can express this type in the following way.
|
||||
//
|
||||
//--------------------------------------------------------------------------------------------
|
||||
|
||||
const T = Type.Object({
|
||||
// const T = {
|
||||
id: Type.String(), // type: 'object',
|
||||
name: Type.String(), // properties: {
|
||||
timestamp: Type.Integer(), // id: {
|
||||
}); // type: 'string'
|
||||
// },
|
||||
// name: {
|
||||
// type: 'string'
|
||||
// },
|
||||
// timestamp: {
|
||||
// type: 'integer'
|
||||
// }
|
||||
// },
|
||||
// required: [
|
||||
// 'id',
|
||||
// 'name',
|
||||
// 'timestamp'
|
||||
// ]
|
||||
// }
|
||||
|
||||
//--------------------------------------------------------------------------------------------
|
||||
//
|
||||
// ... then infer back to the original static type this way.
|
||||
//
|
||||
//--------------------------------------------------------------------------------------------
|
||||
|
||||
type T = Static<typeof T>; // type T = {
|
||||
// id: string,
|
||||
// name: string,
|
||||
// timestamp: number
|
||||
// }
|
||||
|
||||
//--------------------------------------------------------------------------------------------
|
||||
//
|
||||
// ... or use the type to parse JavaScript values.
|
||||
//
|
||||
//--------------------------------------------------------------------------------------------
|
||||
|
||||
import { Value } from "@alkdev/typebox/value";
|
||||
|
||||
const R = Value.Parse(T, value); // const R: {
|
||||
// id: string,
|
||||
// name: string,
|
||||
// timestamp: number
|
||||
// }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
[Back to Home](../readme.md)
|
||||
Reference in New Issue
Block a user