1- export function exactlyOne < T > ( input : ReadonlyArray < T > , property : string ) : T {
1+ export function exactlyOne < T > (
2+ input : ReadonlyArray < T > ,
3+ property : string ,
4+ extraDescription : string = ''
5+ ) : T {
26 if ( input . length !== 1 ) {
37 throw new Error (
4- `Expected exactly one element for ${ property } , got ${ input . length } `
8+ `Expected exactly one ${ property } element , got ${ input . length } . ${ extraDescription } `
59 ) ;
610 }
711
@@ -10,11 +14,12 @@ export function exactlyOne<T>(input: ReadonlyArray<T>, property: string): T {
1014
1115export function optionalOne < T > (
1216 input : ReadonlyArray < T > ,
13- property : string
17+ property : string ,
18+ extraDescription : string = ''
1419) : T | undefined {
1520 if ( input . length > 1 ) {
1621 throw new Error (
17- `Expected one optional element for ${ property } , got ${ input . length } `
22+ `Expected one optional ${ property } element , got ${ input . length } . ${ extraDescription } `
1823 ) ;
1924 }
2025
@@ -23,47 +28,66 @@ export function optionalOne<T>(
2328
2429export function required < T > (
2530 input : T | undefined ,
26- property : string
31+ property : string ,
32+ extraDescription : string = ''
2733) : NonNullable < T > {
2834 if ( input == null ) {
29- throw new Error ( `Expected a value for ${ property } , got undefined` ) ;
35+ throw new Error (
36+ `Expected a value for ${ property } , got undefined. ${ extraDescription } `
37+ ) ;
3038 }
3139
3240 return input ;
3341}
3442
3543export function allRequired < T > (
3644 input : ReadonlyArray < T | undefined > ,
37- property : string
45+ property : string ,
46+ extraDescription : string = ''
3847) : Array < NonNullable < T > > {
39- return input . map ( ( v , i ) => required ( v , `${ property } [${ i } ]` ) ) ;
48+ return input . map ( ( v , i ) =>
49+ required ( v , `${ property } [${ i } ]` , extraDescription )
50+ ) ;
4051}
4152
4253export function atLeastOne < T > (
4354 input : ReadonlyArray < T > ,
44- property : string
55+ property : string ,
56+ extraDescription : string = ''
4557) : ReadonlyArray < T > {
4658 if ( input . length === 0 ) {
47- throw new Error ( `Expected at least one element for ${ property } ` ) ;
59+ throw new Error (
60+ `Expected at least one ${ property } element. ${ extraDescription } `
61+ ) ;
4862 }
4963
5064 return input ;
5165}
5266
5367export function atLeastOneAndAllRequired < T > (
5468 input : ReadonlyArray < T | undefined > ,
55- property : string
69+ property : string ,
70+ extraDescription : string = ''
5671) : ReadonlyArray < NonNullable < T > > {
57- return atLeastOne ( allRequired ( input , property ) , property ) ;
72+ return atLeastOne (
73+ allRequired ( input , property , extraDescription ) ,
74+ property ,
75+ extraDescription
76+ ) ;
5877}
5978
60- export function valueForKey < T > ( input : Record < string , T > , key : string ) : T {
61- return required ( input [ key ] , key ) ;
79+ export function valueForKey < T > (
80+ input : Record < string , T > ,
81+ key : string ,
82+ extraDescription : string = ''
83+ ) : T {
84+ return required ( input [ key ] , key , extraDescription ) ;
6285}
6386
6487export function valuesForKeys < T > (
6588 input : Record < string , T > ,
66- keys : string [ ]
89+ keys : string [ ] ,
90+ extraDescription : string = ''
6791) : T [ ] {
68- return keys . map ( ( key ) => valueForKey ( input , key ) ) ;
92+ return keys . map ( ( key ) => valueForKey ( input , key , extraDescription ) ) ;
6993}
0 commit comments