Skip to content

Commit

Permalink
fix: strengthen typings (#148)
Browse files Browse the repository at this point in the history
* Strengthen Typings

* handle tests

* update typescript
  • Loading branch information
Lioness100 authored Oct 28, 2021
1 parent bf0adfd commit 68b4db1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
28 changes: 15 additions & 13 deletions env-var.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,24 +262,24 @@ interface IOptionalVariable<Exs extends Extensions = {}> extends VariableAccesso

export class EnvVarError extends Error {}

interface IEnv<PresentVariable, OptionalVariable> {
interface IEnv<OptionalVariable, Container> {
/**
* Returns an object containing all current environment variables
*/
get (): {[varName: string]: string},
get (): Container,

/**
* Gets an environment variable that is possibly not set to a value
*/
get (varName: string): OptionalVariable;
get (varName: keyof Container): OptionalVariable;

/**
* Returns a new env-var instance, where the given object is used for the environment variable mapping.
* Use this when writing unit tests or in environments outside node.js.
*/
from<T extends Extensions>(values: NodeJS.ProcessEnv, extensions?: T, logger?: LoggerFn): IEnv<
IPresentVariable<T> & ExtenderType<T>,
IOptionalVariable<T> & ExtenderTypeOptional<T>
from<V, T extends Extensions>(values: V, extensions?: T, logger?: LoggerFn): IEnv<
IOptionalVariable<T> & ExtenderTypeOptional<T>,
V
>;

accessors: PublicAccessors
Expand All @@ -299,14 +299,16 @@ type ExtenderTypeOptional<T extends Extensions> = { [P in keyof T]: (...args: Re
export type Extensions = {
[key: string]: ExtensionFn<any>
}

export type LoggerFn = (varname: string, str: string) => void
export type RaiseErrorFn = (error: string) => void
export type ExtensionFn<T> = (value: string, ...args: any[]) => T
export function get(): {[varName: string]: string}
export function get(varName: string): IOptionalVariable;
export function from<T extends Extensions>(values: NodeJS.ProcessEnv, extensions?: T, logger?: LoggerFn): IEnv<
IPresentVariable<T> & ExtenderType<T>,
IOptionalVariable<T> & ExtenderTypeOptional<T>
>;

export const accessors: PublicAccessors
export const EnvVarError: EnvVarError

export function logger (varname: string, str: string): void
export let accessors: PublicAccessors

type IDefaultEnv = IEnv<IOptionalVariable, NodeJS.ProcessEnv>
export const get: IDefaultEnv['get']
export const from: IDefaultEnv['from']
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"mocha-lcov-reporter": "~1.3.0",
"nyc": "~15.1.0",
"standard": "~14.3.4",
"typescript": "~3.8.3"
"typescript": "~3.9.0"
},
"engines": {
"node": ">=10"
Expand Down
2 changes: 2 additions & 0 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('typescript tests', () => {
// env-var instance with no vars
const e = env.from({})

// @ts-expect-error `PATH` is not in container object
expect(e.get('PATH').asString()).to.equal(undefined)
})
})
Expand All @@ -28,6 +29,7 @@ describe('typescript tests', () => {
const e = env.from({})

expect(() => {
// @ts-expect-error `A_MISSING_VARIABLE` is not in container object
e.get('A_MISSING_VARIABLE').required().asString()
}).to.throw('env-var: "A_MISSING_VARIABLE" is a required variable, but it was not set')
})
Expand Down

0 comments on commit 68b4db1

Please sign in to comment.