Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(type): allow type annotation options to be extracted #551

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions packages/type/src/reflection/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,9 @@ export interface EntityOptions {
* const param1 = typeToObject(data[0]); //yes
* ```
*/
export type TypeAnnotation<T extends string, Options = never> = { __meta?: never & [T, Options] };
export type TypeAnnotation<T extends string, Options = never> = { __meta?: [T, never | Options] };

export type ExtractTypeAnnotationOptions<T extends TypeAnnotation<string, any>> = Exclude<NonNullable<T['__meta']>[1], never>;

/**
* Type to decorate an interface/object literal with entity information.
Expand Down Expand Up @@ -1994,34 +1996,25 @@ export function registerTypeDecorator(decorator: TypeDecorator) {
* type MyAnnotation1<T> = TypeAnnotation<'myAnnotation', T>
*
* //under the hood it is:
* type lowLevel1 = { __meta?: never & ['myAnnotation'] }
* type lowLevel2<T> = { __meta?: never & ['myAnnotation', T] }
* type lowLevel1 = { __meta?: ['myAnnotation', never] }
* type lowLevel2<T> = { __meta?: ['myAnnotation', never | T] }
* ```
*/
export function getAnnotationMeta(type: TypeObjectLiteral): { id: string, params: Type[] } | undefined {
const meta = getProperty(type, '__meta');
if (!meta || !meta.optional) return;
let tuple: TypeTuple | undefined = undefined;

if (meta.type.kind === ReflectionKind.intersection) {
if (meta.type.types.length === 1 && meta.type.types[0].kind === ReflectionKind.tuple) {
tuple = meta.type.types[0] as TypeTuple;
}
if (!tuple && meta.type.types.length === 2) {
tuple = meta.type.types.find(v => v.kind === ReflectionKind.tuple) as TypeTuple | undefined;
if (tuple && !meta.type.types.find(v => v.kind === ReflectionKind.never)) {
tuple = undefined;
}
}
} else if (meta.type.kind === ReflectionKind.tuple) {
tuple = meta.type;
}
if (!meta || !meta.optional) return;
if (meta.type.kind !== ReflectionKind.tuple) return;

if (!tuple) return;
const id = meta.type.types[0];

const id = tuple.types[0];
if (!id || id.type.kind !== ReflectionKind.literal || 'string' !== typeof id.type.literal) return;
const params = tuple.types.slice(1).map(v => v.type);

const options = meta.type.types[1];
if (options.type.kind !== ReflectionKind.union) return;
if (options.type.types[0].kind !== ReflectionKind.never) return;

const params = options.type.types.slice(1);

return { id: id.type.literal, params };
}
Expand Down
34 changes: 34 additions & 0 deletions packages/type/tests/annotations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { test, expect } from '@jest/globals';
import { ExtractTypeAnnotationOptions, getAnnotationMeta, TypeAnnotation, TypeObjectLiteral } from '../src/reflection/type.js';
import { typeOf } from '../src/reflection/reflection.js';
import { expectEqualType } from './utils.js';

test('extract type annotation options', () => {
type Skip = TypeAnnotation<'skip', { if: boolean }>;

type SkipOptions = ExtractTypeAnnotationOptions<Skip>;

const options: SkipOptions = {
if: true,
};
});


test('getAnnotationMeta', () => {
type LevelOptions = { do: boolean };

const levelOptionsType = typeOf<LevelOptions>();

type Level = TypeAnnotation<'level', LevelOptions>;

const levelType = typeOf<Level>() as TypeObjectLiteral;

const meta = getAnnotationMeta(levelType);

expect(meta).toBeDefined();

expect(meta!.id).toBe('level');

expectEqualType(meta!.params[0], levelOptionsType);

});
2 changes: 1 addition & 1 deletion packages/type/tests/integration4.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import { expect, test } from '@jest/globals';
import { assertType, AutoIncrement, Group, groupAnnotation, PrimaryKey, ReflectionKind } from '../src/reflection/type.js';
import { assertType, AutoIncrement, ExtractTypeAnnotationOptions, Group, groupAnnotation, PrimaryKey, ReflectionKind, TypeAnnotation } from '../src/reflection/type.js';
import { typeOf } from '../src/reflection/reflection.js';
import { cast } from '../src/serializer-facade.js';

Expand Down
Loading