@atscript/typescript 0.1.21 → 0.1.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/utils.d.ts
CHANGED
|
@@ -48,11 +48,7 @@ interface TValidatorPluginContext {
|
|
|
48
48
|
* @typeParam T - The annotated type definition.
|
|
49
49
|
* @typeParam DataType - The TypeScript type that `validate` narrows to (auto-inferred).
|
|
50
50
|
*/
|
|
51
|
-
declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = T
|
|
52
|
-
type: {
|
|
53
|
-
__dataType?: infer D;
|
|
54
|
-
};
|
|
55
|
-
} ? unknown extends D ? T extends new (...args: any[]) => infer I ? I : unknown : D : unknown> {
|
|
51
|
+
declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = TAtscriptDataType<T>> {
|
|
56
52
|
protected readonly def: T;
|
|
57
53
|
protected opts: TValidatorOptions;
|
|
58
54
|
constructor(def: T, opts?: Partial<TValidatorOptions>);
|
|
@@ -148,6 +144,26 @@ interface TAtscriptTypeFinal<DataType = unknown> {
|
|
|
148
144
|
type InferDataType<T> = T extends {
|
|
149
145
|
__dataType?: infer D;
|
|
150
146
|
} ? D : unknown;
|
|
147
|
+
/**
|
|
148
|
+
* Extract the DataType from a {@link TAtscriptAnnotatedType}.
|
|
149
|
+
*
|
|
150
|
+
* Resolves the phantom `__dataType` carried by the type definition.
|
|
151
|
+
* When `__dataType` is `unknown` (unset), falls back to the constructor
|
|
152
|
+
* instance type if `T` is also a class (i.e. a generated interface).
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* import type { TAtscriptDataType } from '@atscript/typescript/utils'
|
|
157
|
+
* import MyInterface from './my-interface.as'
|
|
158
|
+
*
|
|
159
|
+
* type Data = TAtscriptDataType<typeof MyInterface>
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
type TAtscriptDataType<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType> = T extends {
|
|
163
|
+
type: {
|
|
164
|
+
__dataType?: infer D;
|
|
165
|
+
};
|
|
166
|
+
} ? unknown extends D ? T extends new (...args: any[]) => infer I ? I : unknown : D : unknown;
|
|
151
167
|
/** Union of all possible type definition shapes. */
|
|
152
168
|
type TAtscriptTypeDef<DataType = unknown> = TAtscriptTypeComplex<DataType> | TAtscriptTypeFinal<DataType> | TAtscriptTypeArray<DataType> | TAtscriptTypeObject<string, DataType>;
|
|
153
169
|
/**
|
|
@@ -496,4 +512,4 @@ declare function serializeAnnotatedType(type: TAtscriptAnnotatedType, options?:
|
|
|
496
512
|
declare function deserializeAnnotatedType(data: TSerializedAnnotatedType): TAtscriptAnnotatedType;
|
|
497
513
|
|
|
498
514
|
export { SERIALIZE_VERSION, Validator, ValidatorError, annotate, buildJsonSchema, createDataFromAnnotatedType, defineAnnotatedType, deserializeAnnotatedType, flattenAnnotatedType, forAnnotatedType, fromJsonSchema, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType, serializeAnnotatedType, throwFeatureDisabled };
|
|
499
|
-
export type { InferDataType, TAnnotatedTypeHandle, TAtscriptAnnotatedType, TAtscriptAnnotatedTypeConstructor, TAtscriptTypeArray, TAtscriptTypeComplex, TAtscriptTypeDef, TAtscriptTypeFinal, TAtscriptTypeObject, TCreateDataOptions, TFlattenOptions, TMetadataMap, TProcessAnnotationContext, TSerializeOptions, TSerializedAnnotatedType, TSerializedAnnotatedTypeInner, TSerializedTypeArray, TSerializedTypeComplex, TSerializedTypeDef, TSerializedTypeFinal, TSerializedTypeObject, TValidatorOptions, TValidatorPlugin, TValidatorPluginContext, TValueResolver };
|
|
515
|
+
export type { InferDataType, TAnnotatedTypeHandle, TAtscriptAnnotatedType, TAtscriptAnnotatedTypeConstructor, TAtscriptDataType, TAtscriptTypeArray, TAtscriptTypeComplex, TAtscriptTypeDef, TAtscriptTypeFinal, TAtscriptTypeObject, TCreateDataOptions, TFlattenOptions, TMetadataMap, TProcessAnnotationContext, TSerializeOptions, TSerializedAnnotatedType, TSerializedAnnotatedTypeInner, TSerializedTypeArray, TSerializedTypeComplex, TSerializedTypeDef, TSerializedTypeFinal, TSerializedTypeObject, TValidatorOptions, TValidatorPlugin, TValidatorPluginContext, TValueResolver };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/typescript",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.23",
|
|
4
4
|
"description": "Atscript: typescript-gen support.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"annotations",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"vitest": "3.2.4"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"@atscript/core": "^0.1.
|
|
67
|
+
"@atscript/core": "^0.1.23"
|
|
68
68
|
},
|
|
69
69
|
"build": [
|
|
70
70
|
{},
|
|
@@ -300,6 +300,55 @@ import { isPhantomType } from '@atscript/typescript/utils'
|
|
|
300
300
|
isPhantomType(someProperty) // true if designType === 'phantom'
|
|
301
301
|
```
|
|
302
302
|
|
|
303
|
+
## `TAtscriptDataType<T>` — Extract DataType from Annotated Type
|
|
304
|
+
|
|
305
|
+
Utility type that extracts the underlying data shape from a `TAtscriptAnnotatedType`. This is the primary way to obtain a TypeScript data type from an Atscript-generated class, especially useful in generic contexts.
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
import type { TAtscriptDataType } from '@atscript/typescript/utils'
|
|
309
|
+
import { Product } from './product.as'
|
|
310
|
+
|
|
311
|
+
type ProductData = TAtscriptDataType<typeof Product>
|
|
312
|
+
// ProductData = { name: string; price: number; tags: string[] }
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
### How It Resolves
|
|
316
|
+
|
|
317
|
+
1. Extracts the phantom `__dataType` from the type definition
|
|
318
|
+
2. If `__dataType` is `unknown` (unset), falls back to the constructor's instance type (`T extends new (...) => infer I`)
|
|
319
|
+
3. Otherwise returns `unknown`
|
|
320
|
+
|
|
321
|
+
### Use in Generics
|
|
322
|
+
|
|
323
|
+
`TAtscriptDataType` is designed for generic code that needs to derive data types from annotated type parameters:
|
|
324
|
+
|
|
325
|
+
```ts
|
|
326
|
+
import type { TAtscriptAnnotatedType, TAtscriptDataType } from '@atscript/typescript/utils'
|
|
327
|
+
|
|
328
|
+
// Generic repository that infers its entity type
|
|
329
|
+
class Repository<T extends TAtscriptAnnotatedType> {
|
|
330
|
+
findOne(id: string): Promise<TAtscriptDataType<T>> { /* ... */ }
|
|
331
|
+
insertOne(data: TAtscriptDataType<T>): Promise<void> { /* ... */ }
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Usage — DataType is automatically inferred
|
|
335
|
+
const repo = new Repository<typeof Product>()
|
|
336
|
+
const product = await repo.findOne('123') // typed as Product
|
|
337
|
+
|
|
338
|
+
// Generic function
|
|
339
|
+
function validate<T extends TAtscriptAnnotatedType>(
|
|
340
|
+
schema: T,
|
|
341
|
+
data: unknown
|
|
342
|
+
): data is TAtscriptDataType<T> {
|
|
343
|
+
return schema.validator().validate(data, true)
|
|
344
|
+
}
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### Difference from `InferDataType`
|
|
348
|
+
|
|
349
|
+
- `TAtscriptDataType<T>` — operates on `TAtscriptAnnotatedType` (the full annotated wrapper). Use this for generated classes and generic code.
|
|
350
|
+
- `InferDataType<T>` — operates on raw type definitions (`TAtscriptTypeDef`, `TAtscriptTypeObject`, etc.). Lower-level, extracts `__dataType` from the type def's phantom generic directly.
|
|
351
|
+
|
|
303
352
|
## Type Exports
|
|
304
353
|
|
|
305
354
|
Key types you may need to import:
|
|
@@ -315,7 +364,8 @@ import type {
|
|
|
315
364
|
TAtscriptTypeComplex, // union/intersection/tuple type def
|
|
316
365
|
TMetadataMap, // typed metadata map
|
|
317
366
|
TAnnotatedTypeHandle, // fluent builder handle
|
|
318
|
-
InferDataType, // extract DataType from a type def
|
|
367
|
+
InferDataType, // extract DataType from a type def's phantom generic
|
|
368
|
+
TAtscriptDataType, // extract DataType from TAtscriptAnnotatedType
|
|
319
369
|
TValidatorOptions, // validator config
|
|
320
370
|
TValidatorPlugin, // plugin function type
|
|
321
371
|
TValidatorPluginContext, // plugin context
|