Skip to content

Commit

Permalink
improvement: do not accept "propertyName" and "type" via decorators
Browse files Browse the repository at this point in the history
Both are computed from reflection and should not be customizable
  • Loading branch information
thetutlage committed Apr 23, 2021
1 parent af68f40 commit f89afe7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
8 changes: 3 additions & 5 deletions src/Decorators/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@

import { CommandArg, ArgTypes, CommandConstructorContract } from '../Contracts'

type DecoratorArg = Partial<Pick<CommandArg, Exclude<keyof CommandArg, 'type'>>>

/**
* Adds arg to the list of command arguments with pre-defined
* type.
*/
function addArg(type: ArgTypes, options: DecoratorArg) {
function addArg(type: ArgTypes, options: Partial<Omit<CommandArg, 'type' | 'propertyName'>>) {
return function arg(target: any, propertyName: string) {
const Command = target.constructor as CommandConstructorContract
Command.boot()
Expand All @@ -27,15 +25,15 @@ export const args = {
/**
* Define argument that accepts string value
*/
string(options?: Partial<CommandArg>) {
string(options?: Partial<Omit<CommandArg, 'type' | 'propertyName'>>) {
return addArg('string', options || {})
},

/**
* Define argument that accepts multiple values. Must be
* the last argument.
*/
spread(options?: Partial<CommandArg>) {
spread(options?: Partial<Omit<CommandArg, 'type' | 'propertyName'>>) {
return addArg('spread', options || {})
},
}
17 changes: 9 additions & 8 deletions src/Decorators/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

import { CommandFlag, FlagTypes, CommandConstructorContract } from '../Contracts'

type DecoratorFlag<ReturnType extends any> = Partial<Omit<CommandFlag<ReturnType>, 'type'>>

/**
* Pushes flag to the list of command flags with predefined
* types.
*/
function addFlag<T extends any>(type: FlagTypes, options: DecoratorFlag<T>) {
function addFlag<T extends any>(
type: FlagTypes,
options: Partial<Omit<CommandFlag<T>, 'propertyName' | 'type'>>
) {
return function flag<TKey extends string, TTarget extends { [K in TKey]: T }>(
target: TTarget,
propertyName: TKey
Expand All @@ -30,35 +31,35 @@ export const flags = {
/**
* Create a flag that excepts string values
*/
string<T extends any>(options?: DecoratorFlag<T>) {
string<T extends any>(options?: Partial<Omit<CommandFlag<T>, 'propertyName' | 'type'>>) {
return addFlag<T>('string', options || {})
},

/**
* Create a flag that excepts numeric values
*/
number<T extends any>(options?: DecoratorFlag<T>) {
number<T extends any>(options?: Partial<Omit<CommandFlag<T>, 'propertyName' | 'type'>>) {
return addFlag<T>('number', options || {})
},

/**
* Create a flag that excepts boolean values
*/
boolean<T extends any>(options?: DecoratorFlag<T>) {
boolean<T extends any>(options?: Partial<Omit<CommandFlag<T>, 'propertyName' | 'type'>>) {
return addFlag<T>('boolean', options || {})
},

/**
* Create a flag that excepts array of string values
*/
array<T extends any>(options?: DecoratorFlag<T>) {
array<T extends any>(options?: Partial<Omit<CommandFlag<T>, 'propertyName' | 'type'>>) {
return addFlag<T>('array', options || {})
},

/**
* Create a flag that excepts array of numeric values
*/
numArray<T extends any>(options?: DecoratorFlag<T>) {
numArray<T extends any>(options?: Partial<Omit<CommandFlag<T>, 'propertyName' | 'type'>>) {
return addFlag<T>('numArray', options || {})
},
}

0 comments on commit f89afe7

Please sign in to comment.