Skip to content

Instantly share code, notes, and snippets.

@akkunchoi
Created May 5, 2018 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akkunchoi/c49848a41a49074bb1b7f3f3b4ba343c to your computer and use it in GitHub Desktop.
Save akkunchoi/c49848a41a49074bb1b7f3f3b4ba343c to your computer and use it in GitHub Desktop.
function option(name: string): Function;
function option(target: any, propertyName: string, propertyDescriptor?: PropertyDescriptor): void;
function option(...args: any[]): Function|void {
if (args.length >= 2) {
_option(args[0], args[1], args[2]);
return;
} else {
console.log("evaluated", args);
return (...args2: any[]) => {
_option(args2[0], args2[1], args2[2], args[0]);
};
}
}
function _option(target: any, propertyName: string, propertyDescriptor?: PropertyDescriptor, hoge?: any) {
console.log("called", target, propertyName, propertyDescriptor, hoge);
const type = Reflect.getMetadata("design:type", target, propertyName);
switch (type) {
case String:
console.log("String");
break;
case Object:
console.log("Object", type);
break;
case Number:
console.log("Number");
break;
case Boolean:
console.log("Boolean");
break;
case Date:
console.log("Date");
break;
case Function:
console.log("Function", type);
break;
default:
console.log("other", type);
}
}
console.log("----------------------------------------")
console.log("Target1")
/*
evaluated [ 'a' ]
evaluated [ 'b' ]
called Target1 {} year undefined b
Number
called Target1 {} year undefined a
Number
called Target1 {} year undefined undefined
Number
*/
class Target1 {
@option
@option("a")
@option("b")
public year: number;
}
console.log("----------------------------------------")
console.log("Target2")
/*
Target2
called Target2 {} name undefined undefined
String
called Target2 {} age undefined undefined
Number
called Target2 {} created undefined undefined
Date
called Target2 {} admin undefined undefined
Boolean
called Target2 {} year { get: [Function: get year],
set: undefined,
enumerable: false,
configurable: true } undefined
Object function Object() { [native code] }
called Target2 {} run { value: [Function: run],
writable: true,
enumerable: false,
configurable: true } undefined
Function function Function() { [native code] }
*/
class Target2 {
@option
private name: string;
@option
private age: number;
@option
private created: Date;
@option
private admin: boolean;
@option
public get year() {
return 2018;
}
@option
public run() {
}
}
console.log("----------------------------------------")
console.log("Target3")
/*
Target3
called Target3 {} age undefined undefined
Object function Object() { [native code] }
*/
class Target3 {
@option
private age: (string | number);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment