Skip to content

Commit

Permalink
runtime v0.7.0-alpha.0
Browse files Browse the repository at this point in the history
### Added
- `getType(val: any)` it is possible to get type of runtime value,

<dl>
<dd>

```typescript
const someValue: unknown = new Animal();
getType(someValue); // > Type<Animal>
```
This works mainly with classes.
Before #29 is implemented, `@reflect()` decorator, `@reflect` JSDoc tag or `getType<OfThatType>()` is required or there will be no Type metadata.

Native types such as Object `{ foo: "", bar: 5 }`, Array `[ {}, true, 5 ]` and primitive types are supported too; those types are recognized and their properties are parsed at runtime.

*Getters and setter of Objects are recognized.*
\
*Classes without metadata will be parsed as Objects.*

</dd>
</dl>

- `Decorator.getArguments(): Array<any>` - constant literal arguments of decorators are captured,
- `Type.isPrimitive()`,
- partial test coverage (~75%) - issue #28,
- `TypeBuilder`,
- decorator can be CallExpression`@decorator()` or (new) just Identifier `@decorator`
- implementation of #7 - custom Property and Method decorators supporting `getType<T>()` of generic parameters, like already supported class decorators.

### Changed
- JSDoc tags @reflectGeneric and @reflectDecorator removed in favor of single @reflect,
- `Property.decorators` changed to `Property.getDecorators()` - to keep it same as `Type.getDecorators()` and `Method.getDecorators()`,
- `Type.flattenInheritedMembers()` support base union and intersection types,
- fixed issue #27,
- fix of some circular dependencies in runtime package,
- few other small bug fixes.
  • Loading branch information
Hookyns committed Feb 13, 2022
1 parent 5040a4c commit 2f65fd6
Show file tree
Hide file tree
Showing 47 changed files with 8,324 additions and 344 deletions.
37 changes: 35 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,40 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[//]: # (### Added)
[//]: # (### Changed)

## [0.6.0-beta.5] - 2022-02-20
## [0.7.0-alpha.0] - 2022-02-13
### Added
- `getType(val: any)` it is possible to get type of runtime value,

<dl>
<dd>

```typescript
const someValue: unknown = new Animal();
getType(someValue); // > Type<Animal>
```
This works mainly with classes.
Before #29 is implemented, `@reflect()` decorator, `@reflect` JSDoc tag or `getType<OfThatType>()` is required or there will be no Type metadata.

Native types such as Object `{ foo: "", bar: 5 }`, Array `[ {}, true, 5 ]` and primitive types are supported too; those types are recognized and their properties are parsed at runtime.

*Getters and setter of Objects are recognized.*
\
*Classes without metadata will be parsed as Objects.*

</dd>
</dl>

- `Decorator.getArguments(): Array<any>` - constant literal arguments of decorators are captured,
- `Type.isPrimitive()`,
- partial test coverage (~75%) - issue #28,
- `TypeBuilder`,
- decorator can be CallExpression`@decorator()` or (new) just Identifier `@decorator`
- implementation of #7 - custom Property and Method decorators supporting `getType<T>()` of generic parameters, like already supported class decorators.

### Changed
- JSDoc tags @reflectGeneric and @reflectDecorator removed in favor of single @reflect
- JSDoc tags @reflectGeneric and @reflectDecorator removed in favor of single @reflect,
- `Property.decorators` changed to `Property.getDecorators()` - to keep it same as `Type.getDecorators()` and `Method.getDecorators()`,
- `Type.flattenInheritedMembers()` support base union and intersection types,
- fixed issue #27,
- fix of some circular dependencies in runtime package,
- few other small bug fixes.
47 changes: 40 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
[![tst-reflect](https://img.shields.io/npm/v/tst-reflect.svg?color=brightgreen&style=flat-square&logo=npm&label=tst-reflect)](https://www.npmjs.com/package/tst-reflect)
[![tst-reflect-transformer](https://img.shields.io/npm/v/tst-reflect-transformer.svg?color=brightgreen&style=flat-square&logo=npm&label=tst-reflect-transformer)](https://www.npmjs.com/package/tst-reflect-transformer)
[![License MIT](https://img.shields.io/badge/License-MIT-brightgreen?style=flat-square)](https://opensource.org/licenses/MIT)
![License MIT](./coverage/badge.svg)

[![Readme Card](https://github-readme-stats.vercel.app/api/pin/?username=hookyns&repo=ts-reflection&theme=tokyonight)](https://github.com/Hookyns/ts-reflection)

[Examples](#examples) | [Synopsis](#synopsis) | [How to start](#how-to-start) | [How does it work](#how-does-it-work) | [Configuration [wiki]](https://github.com/Hookyns/ts-reflection/wiki/Configuration)
[Examples](#examples) | [Synopsis](#synopsis) | [How to start](#how-to-start) | [How does it work](#how-does-it-work) | [Configuration [wiki]](https://github.com/Hookyns/ts-reflection/wiki/Configuration) | [Changelog](https://github.com/Hookyns/ts-reflection/blob/main/CHANGELOG.md)

## About

Expand Down Expand Up @@ -90,12 +91,12 @@ baz: Date

### Decorator With Reflected Generic Type

`tst-reflect-transformer` is able to process class decorators marked by @reflectDecorator JSDoc tag.
`tst-reflect-transformer` is able to process class decorators marked by @reflect JSDoc tag.
You will be able to get `Type` of each decorated class.

```typescript
/**
* @reflectDecorator
* @reflect
*/
export function inject<TType>()
{
Expand Down Expand Up @@ -515,7 +516,18 @@ getType({
*/
export declare class Type
{
static readonly Object: Type;
static readonly Object: Type;
static readonly Unknown: Type;
static readonly Any: Type;
static readonly Void: Type;
static readonly String: Type;
static readonly Number: Type;
static readonly BigInt: Type;
static readonly Boolean: Type;
static readonly Date: Type;
static readonly Null: Type;
static readonly Undefined: Type;
static readonly Never: Type;
/**
* Returns information about generic conditional type.
*/
Expand All @@ -535,7 +547,7 @@ export declare class Type
/**
* List of underlying types in case Type is union or intersection
*/
get types(): ReadonlyArray<Type> | undefined;
get types(): ReadonlyArray<Type>;
/**
* Constructor function in case Type is class
*/
Expand Down Expand Up @@ -588,6 +600,11 @@ export declare class Type
* @returns {Type | undefined}
*/
static find(filter: (type: Type) => boolean): Type | undefined;
/**
* Returns all Types contained in metadata.
* This method is quite useless with reflection.metadata.type = "inline"; Use "typelib" type.
*/
static getTypes(): Type[];
static get store(): MetadataStore;
/**
* Returns true if types are equals
Expand Down Expand Up @@ -616,9 +633,13 @@ export declare class Type
*/
isUnionOrIntersection(): boolean;
/**
* Check if this is a native type("string", "number", "boolean" etc.)
* Check if this is a native type ("string", "number", "boolean", "Array" etc.)
*/
isNative(): boolean;
/**
* Check if this is a primitive type ("string", "number", "boolean" etc.)
*/
isPrimitive(): boolean;
/**
* Check if this type is a string
*/
Expand Down Expand Up @@ -673,6 +694,18 @@ export declare class Type
* Returns array of decorators
*/
getDecorators(): ReadonlyArray<Decorator>;
/**
* Returns object with all methods and properties from current Type and all methods and properties inherited from base types and interfaces to this Type.
* @return {{properties: {[p: string]: Property}, methods: {[p: string]: Method}}}
*/
flattenInheritedMembers(): {
properties: {
[propertyName: string]: Property;
};
methods: {
[methodName: string]: Method;
};
};
/**
* Determines whether the class represented by the current Type derives from the class represented by the specified Type
* @param {Type} classType
Expand Down Expand Up @@ -944,7 +977,7 @@ export interface EnumInfo {
## Motivation
I'm developing this for own Dependency Injection system, to allow registering and resolving based on types. Something like:
I'm developing this Reflection system for own Dependency Injection system, to allow registering and resolving based on types. Something like:
```
serviceCollection.AddScoped<ILog, Log>();
Expand Down
2 changes: 1 addition & 1 deletion dev/di/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getType } from "tst-reflect";
type ComponentConstructor<T = {}> = new (...args: any[]) => T;

/**
* @reflectDecorator
* @reflect
*/
export function inject<TType>() {
const type = getType<TType>();
Expand Down
Loading

0 comments on commit 2f65fd6

Please sign in to comment.