Skip to content

Commit db5e9ad

Browse files
committed
🐞 fixed typescript errors with a slightly more modern version of typescript
1 parent 5af2f79 commit db5e9ad

File tree

11 files changed

+1792
-2490
lines changed

11 files changed

+1792
-2490
lines changed

package-lock.json

Lines changed: 1392 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
"sinon": "^1.17.2",
3434
"sinon-chai": "^2.8.0",
3535
"source-map-support": "^0.4.0",
36-
"typescript": "^1.7.3"
36+
"typescript": "3.1.1"
3737
},
3838
"dependencies": {
39+
"@types/node": "^12.7.5",
3940
"es6-collections": "^0.5.5",
4041
"reflect-metadata": "^0.1.2"
4142
}

src/containerfactory.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11

2-
import {ContainerFactoryInterface} from "./containerfactoryinterface";
3-
import {Container} from "./container";
4-
import {ContainerResolver} from "./resolver";
5-
import {FactoryMethod} from './definition';
2+
import { ContainerFactoryInterface } from "./containerfactoryinterface";
3+
import { ContainerResolver } from "./resolver";
4+
import { FactoryMethod } from './definition';
65

76
/**
87
* Container factory implementation
98
*/
109
export class ContainerFactory implements ContainerFactoryInterface {
1110

12-
/**
13-
* Container instance
14-
*/
15-
private resolver: ContainerResolver;
11+
/**
12+
* Container instance
13+
*/
14+
private resolver: ContainerResolver;
1615

17-
/**
18-
* @constructor
19-
* @param resolver
20-
*/
21-
public constructor(resolver: ContainerResolver) {
22-
this.resolver = resolver;
23-
}
24-
25-
/**
26-
* Create object using the container. Will create new instance for each call
27-
* @param definition Class or string definition
28-
* @param constructorArgs Optional constructor arguments. Overrides constructor arguments in definition
29-
*/
30-
public make(definition: Function|string, constructorArgs: Array<any>): any {
31-
if (typeof definition === "string" && !this.resolver.hasDefinition(definition)) {
32-
throw new Error(`Unknown definition: ${definition}`)
33-
}
16+
/**
17+
* @constructor
18+
* @param resolver
19+
*/
20+
public constructor(resolver: ContainerResolver) {
21+
this.resolver = resolver;
22+
}
3423

35-
return this.resolver.resolve(definition, FactoryMethod.FACTORY, constructorArgs, false);
24+
/**
25+
* Create object using the container. Will create new instance for each call
26+
* @param definition Class or string definition
27+
* @param constructorArgs Optional constructor arguments. Overrides constructor arguments in definition
28+
*/
29+
public make(definition: Function | string, constructorArgs: Array<any>): any {
30+
if (typeof definition === "string" && !this.resolver.hasDefinition(definition)) {
31+
throw new Error(`Unknown definition: ${definition}`)
3632
}
3733

34+
return this.resolver.resolve(definition, FactoryMethod.FACTORY, constructorArgs, false);
35+
}
36+
3837
}

src/containerfactoryinterface.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
22
* Interface for creating objects from container dynamically
33
*/
4-
export class ContainerFactoryInterface {
5-
/**
6-
* Create object using the container. Will create new instance for each call
7-
* @param definition Class or string definition
8-
* @param constructorArgs Optional constructor arguments. Overrides constructor arguments in definition
9-
*/
10-
public make(definition: Function|string, constructorArgs?: Array<any>): any {};
4+
export abstract class ContainerFactoryInterface {
5+
/**
6+
* Create object using the container. Will create new instance for each call
7+
* @param definition Class or string definition
8+
* @param constructorArgs Optional constructor arguments. Overrides constructor arguments in definition
9+
*/
10+
public abstract make(definition: Function | string, constructorArgs?: Array<any>): any;
1111
}

src/decorators.ts

Lines changed: 107 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,137 @@
11
import 'reflect-metadata';
22

3-
import {FactoryMethod} from './definition';
3+
import { FactoryMethod } from './definition';
44

55
/**
66
* Decorator for class constructor injection
77
*/
8-
export function ConstructorInject(targetOrFactoryMethodOrLiteral: any, factoryMethod?: FactoryMethod) {
9-
let method: FactoryMethod = undefined;
8+
export function ConstructorInject(targetOrFactoryMethodOrLiteral: any, factoryMethod?: FactoryMethod) {
9+
let method: FactoryMethod | undefined = undefined;
1010

11-
switch (typeof targetOrFactoryMethodOrLiteral) {
12-
// Simple @ConstructorInject at the top of class
13-
case 'function':
14-
Reflect.defineMetadata('inject:constructor', true, targetOrFactoryMethodOrLiteral);
15-
return targetOrFactoryMethodOrLiteral;
16-
// constructor (@ConstructorInject(FactoryMethod) param1: class, ...);
17-
case 'number':
18-
if (typeof targetOrFactoryMethodOrLiteral === 'number') {
19-
method = targetOrFactoryMethodOrLiteral;
20-
}
21-
return function (target: Object, propertyKey: string|symbol, parameterIndex: number) {
22-
// inject:constructor:param1:method
23-
let metadataName = 'inject:constructor:param' + parameterIndex + ':method';
24-
Reflect.defineMetadata(metadataName, method, target);
25-
};
26-
// constructor (@ConstructorInject('literal',FactoryMethod) param1: class, ...);
27-
case 'string':
28-
let literal: string = targetOrFactoryMethodOrLiteral;
29-
method = factoryMethod;
30-
return function (target:Object, propertyKey:string|symbol, parameterIndex:number) {
31-
let metadataLiteralName = 'inject:constructor:param' + parameterIndex + ':literal';
32-
let metadataFactoryName = 'inject:constructor:param' + parameterIndex + ':method';
11+
switch (typeof targetOrFactoryMethodOrLiteral) {
12+
// Simple @ConstructorInject at the top of class
13+
case 'function':
14+
Reflect.defineMetadata('inject:constructor', true, targetOrFactoryMethodOrLiteral);
15+
return targetOrFactoryMethodOrLiteral;
16+
// constructor (@ConstructorInject(FactoryMethod) param1: class, ...);
17+
case 'number':
18+
if (typeof targetOrFactoryMethodOrLiteral === 'number') {
19+
method = targetOrFactoryMethodOrLiteral;
20+
}
21+
return function (target: Object, _propertyKey: string | symbol, parameterIndex: number) {
22+
// inject:constructor:param1:method
23+
let metadataName = 'inject:constructor:param' + parameterIndex + ':method';
24+
Reflect.defineMetadata(metadataName, method, target);
25+
};
26+
// constructor (@ConstructorInject('literal',FactoryMethod) param1: class, ...);
27+
case 'string':
28+
let literal: string = targetOrFactoryMethodOrLiteral;
29+
method = factoryMethod;
30+
return function (target: Object, _propertyKey: string | symbol, parameterIndex: number) {
31+
let metadataLiteralName = 'inject:constructor:param' + parameterIndex + ':literal';
32+
let metadataFactoryName = 'inject:constructor:param' + parameterIndex + ':method';
3333

34-
Reflect.defineMetadata(metadataLiteralName, literal, target);
35-
Reflect.defineMetadata(metadataFactoryName, method, target);
36-
};
37-
}
34+
Reflect.defineMetadata(metadataLiteralName, literal, target);
35+
Reflect.defineMetadata(metadataFactoryName, method, target);
36+
};
37+
}
3838
}
3939

4040
/**
4141
* Decorator for class property injection
4242
*/
43-
export function Inject(targetOrFactoryMethodOrLiteral: any, propertyKeyOrFactoryMethod?: string|symbol|FactoryMethod): any {
44-
// means use default value or take from definition
45-
let method: FactoryMethod = undefined;
46-
// Because @Inject() and @Inject will be different
43+
export function Inject(targetOrFactoryMethodOrLiteral: any, propertyKeyOrFactoryMethod?: string | symbol | FactoryMethod): any {
44+
// means use default value or take from definition
45+
let method: FactoryMethod | undefined = undefined;
46+
// Because @Inject() and @Inject will be different
4747

48-
switch (typeof targetOrFactoryMethodOrLiteral) {
49-
// That's simple @Inject
50-
case 'object':
51-
if (typeof propertyKeyOrFactoryMethod === 'string') {
52-
let propertyKey: string = propertyKeyOrFactoryMethod;
53-
if (!targetOrFactoryMethodOrLiteral.hasOwnProperty(propertyKey)) {
54-
// TS compiler do not output empty properties, so explicitly define it here
55-
Object.defineProperty(targetOrFactoryMethodOrLiteral, propertyKey, {
56-
configurable: true,
57-
enumerable: true,
58-
writable: true,
59-
value: null
60-
});
61-
}
62-
Reflect.defineMetadata("inject:property", method, targetOrFactoryMethodOrLiteral, propertyKey);
63-
}
64-
break;
65-
// @Inject('literal')
66-
case 'string':
67-
let literal: string = targetOrFactoryMethodOrLiteral;
68-
if (typeof propertyKeyOrFactoryMethod === "number") {
69-
method = propertyKeyOrFactoryMethod;
70-
}
71-
return function (target:Object, propertyKey:string) {
72-
if (!target.hasOwnProperty(propertyKey)) {
73-
Object.defineProperty(target, propertyKey, {
74-
configurable: true,
75-
enumerable: true,
76-
writable: true,
77-
value: null
78-
});
79-
}
80-
Reflect.defineMetadata("inject:property", method, target, propertyKey);
81-
Reflect.defineMetadata('inject:property:literal', literal, target, propertyKey);
82-
};
83-
// @Inject(FactoryMethod)
84-
case 'number':
85-
method = targetOrFactoryMethodOrLiteral;
86-
return function (target: Object, propertyKey: string) {
87-
if (!target.hasOwnProperty(propertyKey)) {
88-
Object.defineProperty(target, propertyKey, {
89-
configurable: true,
90-
enumerable: true,
91-
writable: true,
92-
value: null
93-
});
94-
}
95-
Reflect.defineMetadata("inject:property", method, target, propertyKey);
96-
};
97-
}
48+
switch (typeof targetOrFactoryMethodOrLiteral) {
49+
// That's simple @Inject
50+
case 'object':
51+
if (typeof propertyKeyOrFactoryMethod === 'string') {
52+
let propertyKey: string = propertyKeyOrFactoryMethod;
53+
if (!targetOrFactoryMethodOrLiteral.hasOwnProperty(propertyKey)) {
54+
// TS compiler do not output empty properties, so explicitly define it here
55+
Object.defineProperty(targetOrFactoryMethodOrLiteral, propertyKey, {
56+
configurable: true,
57+
enumerable: true,
58+
writable: true,
59+
value: null
60+
});
61+
}
62+
Reflect.defineMetadata("inject:property", method, targetOrFactoryMethodOrLiteral, propertyKey);
63+
}
64+
break;
65+
// @Inject('literal')
66+
case 'string':
67+
let literal: string = targetOrFactoryMethodOrLiteral;
68+
if (typeof propertyKeyOrFactoryMethod === "number") {
69+
method = propertyKeyOrFactoryMethod;
70+
}
71+
return function (target: Object, propertyKey: string) {
72+
if (!target.hasOwnProperty(propertyKey)) {
73+
Object.defineProperty(target, propertyKey, {
74+
configurable: true,
75+
enumerable: true,
76+
writable: true,
77+
value: null
78+
});
79+
}
80+
Reflect.defineMetadata("inject:property", method, target, propertyKey);
81+
Reflect.defineMetadata('inject:property:literal', literal, target, propertyKey);
82+
};
83+
// @Inject(FactoryMethod)
84+
case 'number':
85+
method = targetOrFactoryMethodOrLiteral;
86+
return function (target: Object, propertyKey: string) {
87+
if (!target.hasOwnProperty(propertyKey)) {
88+
Object.defineProperty(target, propertyKey, {
89+
configurable: true,
90+
enumerable: true,
91+
writable: true,
92+
value: null
93+
});
94+
}
95+
Reflect.defineMetadata("inject:property", method, target, propertyKey);
96+
};
97+
}
9898
}
9999

100100
/**
101101
* Specifies optional resolution (Don't throw error if not found)
102102
* @param args
103103
* @constructor
104104
*/
105-
export function Optional(...args: any[]): any {
106-
return function (target: Object, propertyKey: string, parameterIndex: number) {
107-
if (typeof parameterIndex === "undefined") {
108-
// Property @Optional()
109-
Reflect.defineMetadata("inject:property:optional", true, target, propertyKey);
110-
} else {
111-
// Constructor @Optional()
112-
let metadataName = 'inject:constructor:param' + parameterIndex + ':optional';
113-
Reflect.defineMetadata(metadataName, true, target);
114-
}
105+
export function Optional(..._args: any[]): any {
106+
return function (target: Object, propertyKey: string, parameterIndex: number) {
107+
if (typeof parameterIndex === "undefined") {
108+
// Property @Optional()
109+
Reflect.defineMetadata("inject:property:optional", true, target, propertyKey);
110+
} else {
111+
// Constructor @Optional()
112+
let metadataName = 'inject:constructor:param' + parameterIndex + ':optional';
113+
Reflect.defineMetadata(metadataName, true, target);
115114
}
115+
}
116116
}
117117

118118
/**
119119
* Decorator for creating auto-factories
120120
*/
121121
export function Factory(...args: any[]): any {
122-
let target;
122+
let target;
123123

124-
switch (args.length) {
125-
case 1:
126-
target = args[0];
127-
Reflect.defineMetadata('inject:autofactory', true, target);
128-
return target;
129-
case 3:
130-
target = args[0];
131-
let propertyKey = args[1];
132-
Reflect.defineMetadata('inject:factorymethod', true, target, propertyKey);
133-
return args[2];
134-
default:
135-
throw new Error("@Factory decorator is not allowed here");
136-
}
124+
switch (args.length) {
125+
case 1:
126+
target = args[0];
127+
Reflect.defineMetadata('inject:autofactory', true, target);
128+
return target;
129+
case 3:
130+
target = args[0];
131+
let propertyKey = args[1];
132+
Reflect.defineMetadata('inject:factorymethod', true, target, propertyKey);
133+
return args[2];
134+
default:
135+
throw new Error("@Factory decorator is not allowed here");
136+
}
137137
}

0 commit comments

Comments
 (0)