You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: design/website/docs/type/base.md
+12-2Lines changed: 12 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,18 +4,28 @@ Base class for building extended types.
4
4
5
5
## Definition
6
6
7
-
The Base type is a class definition that includes methods which can be overridden. The two primary methods to override are Check and Errors, both of which are invoked by TypeBox validators. By convention, Base type definitions begin with a leading `T` and are paired with a factory function that creates instances of the type. The following example demonstrates how to define a DateType using Base.
7
+
The Base type is a class definition that includes methods which can be overridden. The primary methods to override are Check and Errors, which are called by TypeBox validators, and Clone, which is used by the TypeBox compositor to create new instances of Base when composing with other types.
8
+
9
+
> ⚠️ Important: The Clone method must be overridden; otherwise, a `Clone not implemented` exception may be thrown. This method is used by the type compositor to generate new instances during type composition. Clone is typically required when the Base type is used within computed types such as Partial, Required, Pick, or Omit.
10
+
11
+
By convention, Base type definitions start with a leading T and are paired with a factory function that creates instances of the type. The following example demonstrates how to define a DateType using Base, implementing the required Check, Errors, and Clone methods.
8
12
9
13
```typescript
10
14
exportclassTDateTypeextendsType.Base<Date> {
15
+
// required: Used by validation
11
16
publicoverride Check(value:unknown):valueisDate {
12
17
returnvalueinstanceofDate
13
18
}
19
+
// required: Used by validation
14
20
publicoverride Errors(value:unknown):object[] {
15
21
return!this.Check(value) ? [{ message: 'not a Date'}] : []
Copy file name to clipboardExpand all lines: docs/docs/type/base.html
+12-2Lines changed: 12 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,26 @@
1
1
<h1>Base</h1>
2
2
<p>Base class for building extended types.</p>
3
3
<h2>Definition</h2>
4
-
<p>The Base type is a class definition that includes methods which can be overridden. The two primary methods to override are Check and Errors, both of which are invoked by TypeBox validators. By convention, Base type definitions begin with a leading <code>T</code> and are paired with a factory function that creates instances of the type. The following example demonstrates how to define a DateType using Base. </p>
4
+
<p>The Base type is a class definition that includes methods which can be overridden. The primary methods to override are Check and Errors, which are called by TypeBox validators, and Clone, which is used by the TypeBox compositor to create new instances of Base when composing with other types.</p>
5
+
<blockquote>
6
+
<p>⚠️ Important: The Clone method must be overridden; otherwise, a <code>Clone not implemented</code> exception may be thrown. This method is used by the type compositor to generate new instances during type composition. Clone is typically required when the Base type is used within computed types such as Partial, Required, Pick, or Omit.</p>
7
+
</blockquote>
8
+
<p>By convention, Base type definitions start with a leading T and are paired with a factory function that creates instances of the type. The following example demonstrates how to define a DateType using Base, implementing the required Check, Errors, and Clone methods.</p>
5
9
<pre><codeclass="language-typescript">export class TDateType extends Type.Base<Date> {
10
+
// required: Used by validation
6
11
public override Check(value: unknown): value is Date {
7
12
return value instanceof Date
8
13
}
14
+
// required: Used by validation
9
15
public override Errors(value: unknown): object[] {
10
16
return !this.Check(value) ? [{ message: 'not a Date'}] : []
0 commit comments