8000 added "noImplicitAny" for angular2 quickstart, as that repo by defaul… · hubcarl/json-typescript-mapper@ee29dec · GitHub
[go: up one dir, main page]

Skip to content

Commit ee29dec

Browse files
author
艾伦
committed
added "noImplicitAny" for angular2 quickstart, as that repo by default do not allow implicit any
1 parent 9c6ad3b commit ee29dec

File tree

7 files changed

+74
-16
lines changed

7 files changed

+74
-16
lines changed

index.d.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'reflect-metadata';
2+
import { IDecoratorMetaData } from './';
3+
/**
4+
* provide interface to indicate the object is allowed to be traversed
5+
*
6+
* @interface
7+
*/
8+
export interface IGenericObject {
9+
[key: string]: any;
10+
}
11+
/**
12+
* IDecoratorMetaData<T>
13+
* DecoratorConstraint
14+
*
15+
* @interface
16+
*/
17+
export interface IDecoratorMetaData<T> {
18+
name?: string;
19+
clazz?: {
20+
new (): T;
21+
};
22+
}
23+
/**
24+
* JsonProperty
25+
*
26+
* @function
27+
* @property {IDecoratorMetaData<T>|string} metadata, encapsulate it to DecoratorMetaData for standard use
28+
* @return {(target:Object, targetKey:string | symbol)=> void} decorator function
29+
*/
30+
export declare function JsonProperty<T>(metadata?: IDecoratorMetaData<T> | string): (target: Object, targetKey: string | symbol) => void;
31+
/**
32+
* deserialize
33+
*
34+
* @function
35+
* @param {{new():T}} clazz, class type which is going to initialize and hold a mapping json
36+
* @param {Object} json, input json object which to be mapped
37+
*
38+
* @return {T} return mapped object
39+
*/
40+
export declare function deserialize<T extends IGenericObject>(Clazz: {
41+
new (): T;
42+
}, json: IGenericObject): T;

index.ts

Lines changed: 19 additions & 8 deletions
< 67E6 td data-grid-cell-id="diff-dcdc3e0b3362edb8fec2a51d3fa51f8fb8af8f70247e06d9887fa934834c9122-41-52-1" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">52
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import 'reflect-metadata';
22
import {isTargetType, isPrimitiveOrPrimitiveClass, isArrayOrArrayClass} from './libs/utils';
3+
import {IDecoratorMetaData} from './';
4+
5+
/**
6+
* provide interface to indicate the object is allowed to be traversed
7+
*
8+
* @interface
9+
*/
10+
export interface IGenericObject {
11+
[key: string]: any;
12+
}
13+
314

415
/**
516
* Decorator variable name
@@ -40,10 +51,10 @@ class DecoratorMetaData<T> {
4051
* @return {(target:Object, targetKey:string | symbol)=> void} decorator function
41
*/
4253
export function JsonProperty<T>(metadata?: IDecoratorMetaData<T>|string): (target: Object, targetKey: string | symbol)=> void {
43-
let decoratorMetaData;
54+
let decoratorMetaData: IDecoratorMetaData<T>;
4455

4556
if (isTargetType(metadata, 'string')) {
46-
decoratorMetaData = new DecoratorMetaData(metadata as string);
57+
decoratorMetaData = new DecoratorMetaData<T>(metadata as string);
4758
}
4859
else if (isTargetType(metadata, 'object')) {
4960
decoratorMetaData = metadata as IDecoratorMetaData<T>;
@@ -89,23 +100,23 @@ function getJsonProperty<T>(target: any, propertyKey: string): IDecoratorMetaDat
89100
* @return {IDecoratorMetaData<T>} check if any arguments is null or undefined
90101
*/
91102
function hasAnyNullOrUndefined(...args: any[]) {
92-
return args.some((arg: any)=>arg === null || arg === undefined);
103+
return args.some((arg: any) => arg === null || arg === undefined);
93104
}
94105

95106

96-
function mapFromJson<T>(decoratorMetadata: IDecoratorMetaData<any>, instance: T, json: Object, key: any): any {
107+
function mapFromJson<T>(decoratorMetadata: IDecoratorMetaData<any>, instance: T, json: IGenericObject, key: any): any {
97108
/**
98109
* if decorator name is not found, use target property key as decorator name. It means mapping it directly
99110
*/
100111
let decoratorName = decoratorMetadata.name || key;
101-
let innerJson = json ? json[decoratorName] : undefined;
112+
let innerJson: any = json ? json[decoratorName] : undefined;
102113
let clazz = getClazz(instance, key);
103114
if (isArrayOrArrayClass(clazz)) {
104115
let metadata = getJsonProperty(instance, key);
105116
if (metadata && metadata.clazz || isPrimitiveOrPrimitiveClass(clazz)) {
106117
if (innerJson && isArrayOrArrayClass(innerJson)) {
107118
return innerJson.map(
108-
(item)=> deserialize(metadata.clazz, item)
119+
(item: any) => deserialize(metadata.clazz, item)
109120
);
110121
}
111122
return;
@@ -130,7 +141,7 @@ function mapFromJson<T>(decoratorMetadata: IDecoratorMetaData<any>, instance: T,
130141
*
131142
* @return {T} return mapped object
132143
*/
133-
export function deserialize<T>(Clazz: {new(): T}, json: Object): T {
144+
export function deserialize<T extends IGenericObject>(Clazz: {new(): T}, json: IGenericObject): T {
134145
/**
135146
* As it is a recursive function, ignore any arguments that are unset
136147
*/
@@ -149,7 +160,7 @@ export function deserialize<T>(Clazz: {new(): T}, json: Object): T {
149160
*/
150161
let instance = new Clazz();
151162

152-
Object.keys(instance).forEach((key: any) => {
163+
Object.keys(instance).forEach((key: string) => {
153164
/**
154165
* get decoratorMetaData, structure: { name?:string, clazz?:{ new():T } }
155166
*/

libs/utils.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export declare function isTargetType(val: any, type: "object" | "string"): boolean;
2+
export declare function isPrimitiveOrPrimitiveClass(obj: any): boolean;
3+
export declare function isArrayOrArrayClass(clazz: Function): boolean;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"mocha": "2.0.1"
1313
},
1414
"scripts": {
15+
"typings:generate": "tsc --declaration",
1516
"test": "mocha ./spec/index.js"
1617
},
1718
"description": "For single page application, data sources are obtained from API server. Instead of directly using api data, we \r definitely require an adapter layer to transform data as needed. Furthermore, \r the adapter inverse the the data dependency from API server(API Server is considered uncontrollable and \r highly unreliable as data structure may be edit by backend coder for some specific purposes)to our adapter \r which becomes reliable. Thus, this library is created as the adapter make use of es7 reflect decorator.",

spec/index.d.ts

Whitespace-only changes.

spec/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ describe('index()', function () {
5454
"Name": "Mark",
5555
"xing": "Galea",
5656
"age": 30,
57-
"AddressArr": [],
58-
"Address": null
57+
"AddressArr": [] as Array<any>,
58+
"Address": null as any
5959
};
6060
const person = deserialize(Person, json);
6161
expect(person.address).to.be.equals(void 0);
@@ -132,32 +132,32 @@ describe('index()', function () {
132132
});
133133

134134
it('empty json object #2', function () {
135-
let json = null;
135+
let json: any = null;
136136
const person = deserialize(Person, json);
137137
expect(person).to.be.equals(void 0);
138138
});
139139

140140
it('empty json object #3', function () {
141-
let json = void 0;
141+
let json: any = void 0;
142142
const person = deserialize(Person, json);
143143
expect(person).to.be.equals(void 0);
144144
});
145145

146146
it('invalid primitive value #1', function () {
147147
let json = 123;
148-
const person = deserialize(Person, json);
148+
const person = deserialize(Person, json as any);
149149
expect(person).to.be.equals(void 0);
150150
});
151151

152152
it('invalid primitive value #2', function () {
153153
let json = '';
154-
const person = deserialize(Person, json);
154+
const person = deserialize(Person, json as any);
155155
expect(person).to.be.equals(void 0);
156156
});
157157

158158
it('invalid primitive value #3', function () {
159159
let json = NaN;
160-
const person = deserialize(Person, json);
160+
const person = deserialize(Person, json as any);
161161
expect(person).to.be.equals(void 0);
162162
});
163163

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"target": "es5",
55
"sourceMap": true,
66
"experimentalDecorators": true,
7-
"emitDecoratorMetadata": true
7+
"emitDecoratorMetadata": true,
8+
"noImplicitAny": true
89
},
910
"exclude": [
1011
"node_modules"

0 commit comments

Comments
 (0)
0