Skip to content

Commit 97a6a72

Browse files
authored
fix: Correct environment source typing (#100)
* fix: Correct environment source typing When an environment object source contained properties that didn't match with the expected pattern, the entire config was being typed as `never`. * fix: Correct TS issue
1 parent 711f276 commit 97a6a72

4 files changed

Lines changed: 14 additions & 5 deletions

File tree

src/builders/Builder.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { buildEnvironment } from "../buildEnvironment.js";
21
import { DictionaryDataSource } from "../dataSources/DictionaryDataSource.js";
32
import { EnvironmentDataSource } from "../dataSources/EnvironmentDataSource.js";
43
import { FetchedDataSource } from "../dataSources/FetchedDataSource.js";

src/builders/BuilderImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class BuilderImpl {
3535
this.#postMergeFns.push(fn);
3636
}
3737

38-
add(dataSource: IDataSource<Record<string, any>>) {
38+
add(dataSource: IDataSource) {
3939
this.#dsDefs.push({
4040
dataSource: dataSource
4141
});

src/builders/EnvAwareBuilder.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,17 @@ export class EnvAwareBuilder<TEnvironments extends string, T extends Record<stri
3939
}
4040

4141
addEnvironment<TDic extends Record<string, ConfigurationValue>, TPrefix extends string = 'OPT_'>(env: Record<string, ConfigurationValue> | (() => Promise<Record<string, ConfigurationValue>>), prefix: string = 'OPT_') {
42-
return this.add<MergeResult<T, InflateDictionary<TDic, '__', TPrefix>>>(new EnvironmentDataSource(env, prefix));
42+
/*
43+
InflateDictionary is a utility type that does generate a type that is assignable to Record<string, any>, but in
44+
a way that TypeScript does not understand. It uses a trick to merge individually-inflated keys in individual,
45+
single-property Record's into one record.
46+
47+
So ignoring TS2344 for the time being. Maybe it is my TypeScript's lack of ability, or maybe not.
48+
49+
Time will tell.
50+
*/
51+
// @ts-expect-error ts2344
52+
return this.add<InflateDictionary<TDic, '__', TPrefix>>(new EnvironmentDataSource(env, prefix));
4353
}
4454

4555
addFetched<NewT extends Record<string, any>>(input: URL | RequestInfo | (() => Promise<URL | RequestInfo>), required: boolean = true, init?: RequestInit, procesFn?: ProcessFetchResponse<NewT>) {
@@ -51,7 +61,7 @@ export class EnvAwareBuilder<TEnvironments extends string, T extends Record<stri
5161
}
5262

5363
addSingleValue<TKey extends string, TValue extends ConfigurationValue, TSep extends string = ':'>(path: TKey | (() => Promise<[TKey, TValue]>), valueOrHierarchySeparator?: TValue | TSep, hierarchySeparator?: TSep) {
54-
return this.add(new SingleValueDataSource<MergeResult<T, InflateKey<TKey, TValue, TSep>>>(path, valueOrHierarchySeparator, typeof path === 'function' ? valueOrHierarchySeparator as string : hierarchySeparator));
64+
return this.add(new SingleValueDataSource<InflateKey<TKey, TValue, TSep>>(path, valueOrHierarchySeparator, typeof path === 'function' ? valueOrHierarchySeparator as string : hierarchySeparator));
5565
}
5666

5767
postMerge<U extends Record<string, any> = T>(fn: (config: T) => U | Promise<U>): IEnvAwareBuilder<TEnvironments, U> {

src/wj-config.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export type InflateKey<TKey extends string, TValue extends ConfigurationValue, T
3737
} :
3838
{
3939
[K in FullKey]: TValue;
40-
} : never;
40+
} : {};
4141

4242
/**
4343
* Inflates entire dictionaries into their corresponding final objects.

0 commit comments

Comments
 (0)