Skip to content

Helpers for working with typescript's reflection

License

Notifications You must be signed in to change notification settings

SlickyJS/Reflection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM version Build Status

Slicky/Reflection

Helpers for working with typescript's reflection.

Installation

$ npm install @slicky/reflection

Class annotations

Annotation definition:

import {makeClassDecorator} from '@slicky/reflection';

class NamedClassDefinition
{
	
	constructor(name: string) { ... }
	
}

let NamedClass = makeClassDecorator(NamedClassDefinition);

Usage:

import {getAnnotations, findAnnotation} from '@slicky/reflection';

@NamedClass('Test')
class TestClass { ... }

// get all annotations on class:
getAnnotations(TestClass);

// finding annotation for class:
findAnnotation(TestClass, NamedClassDefinition);

##Properties metadata

Properties metadata can be used also for methods and accessors.

Metadata definition:

import {makePropertyDecorator} from '@slicky/reflection';

class NamedPropertyDefinition
{
	
	constructor(name: string) { ... }
	
}

let NamedProperty = makePropertyDecorator(NamedPropertyDefinition);

Usage:

import {getPropertiesMetadata, getPropertyMetadata, findPropertyMetadata} from '@slicky/reflection';

class TestClass
{
	
	@NamedProperty('test')
	prop;
	
}

// get all properties on class:
getPropertiesMetadata(TestClass);

// get all metadata for property "prop":
getPropertyMetadata(TestClass, 'prop');

// finding metadata for property "prop":
findPropertyMetadata(TestClass, NamedPropertyDefinition, 'prop');

Parameters metadata

Metadata definition:

import {makeParameterDecorator} from '@slicky/reflection';

class NamedParameterDefinition
{
	
	constructor(name: string) { ... }
	
}

let NamedParameter = makeParameterDecorator(NamedParameterDefinition);

Usage:

import {getParametersMetadata, findParameterMetadata} from '@slicky/reflection';

class TestClass
{
	
	constructor(@NamedParameter('param') arg) { ... }
	
	method(arg1, @NamedParameter('param2') arg2) { ... }
	
}

// get all parameters in constructor:
getParametersMetadata(TestClass);

// get all parameters in method "method":
getParametersMetadata(TestClass, 'method');

// finding metadata for first argument in constructor:
findParameterMetadata(TestClass, NamedParameterDefinition, 0);

// finding metadata for second argument in method "method":
findParameterMetadata(TestClass, NamedParameterDefinition, 1, 'method');