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 { Rooted , Parser } from './types/common' ;
8+ import type { LazyObject } from './types/lazy' ;
9+ import type { Locator } from './types/locator' ;
10+ import type { MapParser } from './types/map' ;
11+ import type { OptionParser , OptionParserConfig } from './types/option' ;
12+ import type { RootParser , RootPrefixes , ConfigParser } from './types/root' ;
13+ import type { SectionParser , SectionProperties } from './types/section' ;
14+ import type { Map } from './types/utils' ;
515
616/**
717 * Single option
818 */
9- function option ( {
19+ export function option < Value , Result , MappedValue = Value > ( {
1020 defaultValue,
1121 parseCli = _ . identity ,
1222 parseEnv = _ . identity ,
1323 validate = _ . noop ,
1424 map : mapFunc = _ . identity ,
1525 isDeprecated = false
16- } ) {
26+ } : OptionParserConfig < Value , MappedValue , Result > = { } ) : OptionParser < MappedValue , Result > {
27+ const validateFunc : typeof validate = validate ;
28+
1729 return ( locator , parsed ) => {
1830 const config = parsed . root ;
1931 const currNode = locator . parent ? _ . get ( config , locator . parent ) : config ;
2032
21- let value , isSetByUser = true ;
33+ let value : unknown , isSetByUser = true ;
2234 if ( locator . cliOption !== undefined ) {
2335 value = parseCli ( locator . cliOption ) ;
2436 } else if ( locator . envVar !== undefined ) {
@@ -38,7 +50,7 @@ function option({
3850 console . warn ( `Using "${ locator . name } " option is deprecated` ) ;
3951 }
4052
41- validate ( value , config , currNode , { isSetByUser} ) ;
53+ validateFunc ( value , config , currNode , { isSetByUser} ) ;
4254
4355 return mapFunc ( value , config , currNode , { isSetByUser} ) ;
4456 } ;
@@ -48,13 +60,15 @@ function option({
4860 * Object with fixed properties.
4961 * Any unknown property will be reported as error.
5062 */
51- function section ( properties ) {
52- const expectedKeys = _ . keys ( properties ) ;
63+ export function section < Config , Result > ( properties : SectionProperties < Config , Result > ) : SectionParser < Config , Result > {
64+ const expectedKeys = _ . keys ( properties ) as Array < keyof Config > ;
65+
5366 return ( locator , config ) => {
5467 const unknownKeys = _ . difference (
5568 _ . keys ( locator . option ) ,
56- expectedKeys
69+ expectedKeys as Array < string >
5770 ) ;
71+
5872 if ( unknownKeys . length > 0 ) {
5973 throw new UnknownKeysError (
6074 unknownKeys . map ( ( key ) => `${ locator . name } .${ key } ` )
@@ -63,6 +77,7 @@ function section(properties) {
6377
6478 const lazyResult = buildLazyObject ( expectedKeys , ( key ) => {
6579 const parser = properties [ key ] ;
80+
6681 return ( ) => parser ( locator . nested ( key ) , config ) ;
6782 } ) ;
6883
@@ -76,32 +91,34 @@ function section(properties) {
7691 * Object with user-specified keys and values,
7792 * parsed by valueParser.
7893 */
79- function map ( valueParser , defaultValue ) {
94+ export function map < SubConfig , Result > (
95+ valueParser : Parser < SubConfig , Result > ,
96+ defaultValue : Map < SubConfig >
97+ ) : MapParser < Map < SubConfig > , Result > {
8098 return ( locator , config ) => {
8199 if ( locator . option === undefined ) {
82100 if ( ! defaultValue ) {
83- return { } ;
101+ return { } as LazyObject < Map < SubConfig > > ;
84102 }
85103 locator = locator . resetOption ( defaultValue ) ;
86104 }
87105
88- const optionsToParse = Object . keys ( locator . option ) ;
89- const lazyResult = buildLazyObject ( optionsToParse , ( key ) => {
106+ const optionsToParse = Object . keys ( locator . option as Map < SubConfig > ) ;
107+ const lazyResult = buildLazyObject < Map < SubConfig > > ( optionsToParse , ( key ) => {
90108 return ( ) => valueParser ( locator . nested ( key ) , config ) ;
91109 } ) ;
110+
92111 _ . set ( config , locator . name , lazyResult ) ;
93112
94113 return lazyResult ;
95114 } ;
96115}
97116
98- function root ( rootParser , { envPrefix, cliPrefix} ) {
117+ export function root < Config , Result = Config > ( rootParser : RootParser < Config , Result > , { envPrefix, cliPrefix} : RootPrefixes = { } ) : ConfigParser < Config > {
99118 return ( { options, env, argv} ) => {
100119 const rootLocator = initLocator ( { options, env, argv, envPrefix, cliPrefix} ) ;
101- const parsed = { } ;
102- rootParser ( rootLocator , parsed ) ;
103- return forceParsing ( parsed . root ) ;
120+ const parsed = rootParser ( rootLocator as Locator < Config > , { } as Rooted < Result > ) ;
121+
122+ return forceParsing ( parsed ) ;
104123 } ;
105124}
106-
107- module . exports = { option, section, map, root} ;
0 commit comments