1- const _ = require ( 'lodash' ) ;
2- const { buildLazyObject, forceParsing} = require ( './lazy' ) ;
3- const { MissingOptionError, UnknownKeysError} = require ( './errors' ) ;
4- const initLocator = require ( './locator' ) ;
1+ import _ from 'lodash' ;
2+
3+ import { MissingOptionError , UnknownKeysError } from './errors' ;
4+ import { buildLazyObject , forceParsing } from './lazy' ;
5+ import initLocator from './locator' ;
6+
7+ import type { LazyObject } from '../types/lazy' ;
8+ import type { RootParsedConfig } from '../types/common' ;
9+ import type { MapParser } from '../types/map' ;
10+ import type { OptionParser , OptionParserConfig } from '../types/option' ;
11+ import type { RootParser , RootPrefixes , ConfigParser } from '../types/root' ;
12+ import type { SectionParser , SectionProperties } from '../types/section' ;
13+
14+ type Parser < T , R = any > = OptionParser < T , R > | SectionParser < T , R > | MapParser < T , R > ;
515
616/**
717 * Single option
818 */
9- function option ( {
19+ export function option < T , S = T , R = any > ( {
1020 defaultValue,
1121 parseCli = _ . identity ,
1222 parseEnv = _ . identity ,
1323 validate = _ . noop ,
1424 map : mapFunc = _ . identity
15- } ) {
25+ } : OptionParserConfig < T , S , R > ) : OptionParser < S , R > {
26+ const validateFunc : typeof validate = validate ;
27+
1628 return ( locator , parsed ) => {
1729 const config = parsed . root ;
18- const currNode = locator . parent ? _ . get ( config , locator . parent ) : config ;
30+ const currNode = locator . parent ? _ . get ( parsed , locator . parent ) : config ;
1931
20- let value ;
32+ let value : unknown ;
2133 if ( locator . cliOption !== undefined ) {
2234 value = parseCli ( locator . cliOption ) ;
2335 } else if ( locator . envVar !== undefined ) {
@@ -31,7 +43,8 @@ function option({
3143 } else {
3244 throw new MissingOptionError ( locator . name ) ;
3345 }
34- validate ( value , config , currNode ) ;
46+
47+ validateFunc ( value , config , currNode ) ;
3548
3649 return mapFunc ( value , config , currNode ) ;
3750 } ;
@@ -41,13 +54,15 @@ function option({
4154 * Object with fixed properties.
4255 * Any unknown property will be reported as error.
4356 */
44- function section ( properties ) {
45- const expectedKeys = _ . keys ( properties ) ;
57+ export function section < T , R = any > ( properties : SectionProperties < T , R > ) : SectionParser < T , R > {
58+ const expectedKeys = _ . keys ( properties ) as Array < keyof T > ;
59+
4660 return ( locator , config ) => {
4761 const unknownKeys = _ . difference (
4862 _ . keys ( locator . option ) ,
49- expectedKeys
63+ expectedKeys as Array < string >
5064 ) ;
65+
5166 if ( unknownKeys . length > 0 ) {
5267 throw new UnknownKeysError (
5368 unknownKeys . map ( ( key ) => `${ locator . name } .${ key } ` )
@@ -56,6 +71,7 @@ function section(properties) {
5671
5772 const lazyResult = buildLazyObject ( expectedKeys , ( key ) => {
5873 const parser = properties [ key ] ;
74+
5975 return ( ) => parser ( locator . nested ( key ) , config ) ;
6076 } ) ;
6177
@@ -69,17 +85,20 @@ function section(properties) {
6985 * Object with user-specified keys and values,
7086 * parsed by valueParser.
7187 */
72- function map ( valueParser , defaultValue ) {
88+ export function map < T extends Record < string , any > , V extends T [ string ] = T [ string ] , R = any > (
89+ valueParser : Parser < V , R > ,
90+ defaultValue : Record < string , V >
91+ ) : MapParser < Record < string , V > , R > {
7392 return ( locator , config ) => {
7493 if ( locator . option === undefined ) {
7594 if ( ! defaultValue ) {
76- return { } ;
95+ return { } as LazyObject < T > ;
7796 }
7897 locator = locator . resetOption ( defaultValue ) ;
7998 }
8099
81- const optionsToParse = Object . keys ( locator . option ) ;
82- const lazyResult = buildLazyObject ( optionsToParse , ( key ) => {
100+ const optionsToParse = Object . keys ( locator . option as Record < string , V > ) ;
101+ const lazyResult = buildLazyObject < Record < string , V > > ( optionsToParse , ( key ) => {
83102 return ( ) => valueParser ( locator . nested ( key ) , config ) ;
84103 } ) ;
85104 _ . set ( config , locator . name , lazyResult ) ;
@@ -88,13 +107,11 @@ function map(valueParser, defaultValue) {
88107 } ;
89108}
90109
91- function root ( rootParser , { envPrefix, cliPrefix} ) {
110+ export function root < T > ( rootParser : RootParser < T > , { envPrefix, cliPrefix} : RootPrefixes ) : ConfigParser < T > {
92111 return ( { options, env, argv} ) => {
93112 const rootLocator = initLocator ( { options, env, argv, envPrefix, cliPrefix} ) ;
94- const parsed = { } ;
95- rootParser ( rootLocator , parsed ) ;
96- return forceParsing ( parsed . root ) ;
113+ const parsed = rootParser ( rootLocator , { } as RootParsedConfig < T > ) ;
114+
115+ return forceParsing ( parsed ) ;
97116 } ;
98117}
99-
100- module . exports = { option, section, map, root} ;
0 commit comments