Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added strongly typed options interfaces so that your return value and default parameters match the type specified in the options. #230

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dotenv from 'dotenv'
dotenv.config()
import { ArrayOpts, BaseInputValue, BooleanOpts, InputValue, IOpts, IParsedOpts, NumberOpts, StringOpts } from './types'

import { IOpts, IParsedOpts, InputValue } from './types'
dotenv.config()

const VALID_TYPES = [ 'string', 'array', 'boolean', 'number' ]

@@ -59,7 +59,16 @@ const parseValue = (val: string, type: string): InputValue => {
return val.trim()
}

export const getInput = (key: string | Array<string> | IOpts, opts: IOpts): InputValue => {
// eslint-disable-next-line no-unused-vars
export function getInput(key: string | Array<string> | IOpts, opts: BooleanOpts): boolean;
// eslint-disable-next-line no-unused-vars,no-redeclare
export function getInput(key: string | Array<string> | IOpts, opts: StringOpts): string;
// eslint-disable-next-line no-unused-vars,no-redeclare
export function getInput(key: string | Array<string> | IOpts, opts: NumberOpts): number;
// eslint-disable-next-line no-unused-vars,no-redeclare
export function getInput(key: string | Array<string> | IOpts, opts: ArrayOpts): BaseInputValue[];
// eslint-disable-next-line no-redeclare
export function getInput(key: string | Array<string> | IOpts, opts: IOpts): InputValue {
let parsedOptions: IOpts
if (typeof key === 'string' || Array.isArray(key)) {
parsedOptions = {
50 changes: 35 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
/* eslint-disable no-unused-vars */
type ModifierFunction = (val: InputValue) => InputValue

export type InputValue = undefined | string | boolean | number | Array<string | boolean | number>

export interface IOpts {
key?: string | Array<string>
type?: string
required?: boolean,
disableable?: boolean
default?: InputValue
modifier?: ModifierFunction
export type BaseInputValue = string | boolean | number;

export type InputValue = undefined | BaseInputValue | Array<BaseInputValue>

export type IOpts = BooleanOpts | StringOpts | NumberOpts | ArrayOpts | BaseOpts<InputValue>;

export interface BooleanOpts extends BaseOpts<boolean> {
type: 'boolean';
}

export interface StringOpts extends BaseOpts<string> {
type: 'string';
}

export interface NumberOpts extends BaseOpts<number> {
type: 'number';
}

export interface ArrayOpts extends BaseOpts<BaseInputValue[]> {
type: 'array';
}

export interface BaseOpts<T extends InputValue> {
key?: string | Array<string>
type?: string
required?: boolean,
disableable?: boolean
default?: T
modifier?: ModifierFunction
}

export interface IParsedOpts {
key: string | Array<string>
type: string
required: boolean,
disableable: boolean
default?: InputValue
modifier?: ModifierFunction
key: string | Array<string>
type: string
required: boolean,
disableable: boolean
default?: InputValue
modifier?: ModifierFunction
}