Let's start at the end with this snapshot which depicts all unit tests passing but highlighted in red, an unfortunate loss of fidelity in types:
What you are seeing is a Configurator()
function which provides a way to build up a configuration and when you're done you call done()
and the typed configuration is available. The good news is it almost works. It does work at run time (note the blue text which shows that all keys -- a, b, and c -- are indeed set). However, as you see in the circled areas at the bottom, property b
is not typed whereas a
and c
are.
The reason for this miss on typing for b
comes from implementation details which are to some degree understood but let's hit them. Here's the Configurator
code:
export function Configurator<I extends object>(initial?: I) {
let configuration = () => initial || {};
const api = <C extends object>(current: C): IConfigurator<C> => {
return {
set<V, K extends string, KV = { [U in K]: V }>(key: K, value: V) {
const keyValue = ({ [key]: value as V } as unknown) as KV;
const updated = { ...config, ...keyValue };
configuration = (): C & KV => updated;
return api<C & KV>(updated);
},
done() {
return configuration() as C;
},
};
};
return api(initial || {});
}
The interface IConfigurator<T>
is returned on every loop of configuration with the api
surface and using set()
is able to add in new key value pairs as they come in with full Typescript fidelity. This part works and it is why the variable c
remembers the property "a" (see item #1 from above), and why variable d
remembers both property "a" (because c knew about it) and property "c".
Property "b" (set on the line highlighted as #2), is remembered at run time because of the configuration
parameter being assigned in the Configuration's enclosure versus the api
's enclosure. We had originally just assigned in the object value but what you see here is a function which returns a value; we changed this because sometimes Typescript is better at inferring things when a function gets involved. Sadly not here. The configuration type is pegged at being an empty object. Because the api
surface as type C
we can cast the configuration over to type C
but that means that properties like b
get orphaned from the type system's inference.
My question is two fold:
- Is there a way in which we can capture/remember the type of
configuration
so that when we call thedone()
method we can ensure that full type support is available? - Alternatively, if the type for the
configuration
has been lost is there a way to rebuild it? As you can see from the screenshot, at run time the properties and their types can be identified ... seems like this should lead to a means to achieving full type support but attempts at this so far have failed (see below)
Note: here's the attempt we made to achieve #2 from above.
export type AppendToObject<T, U extends keyof any, V> = {
[K in keyof T | U]: K extends keyof T ? T[K] : V;
};
/** Given a structured run-time object, iterate over keys and append types */
export const inferObject = <T extends object>(v: T) => {
let obj = v;
Object.keys(v).forEach((key) => {
const value = (v as any)[key];
obj = fixup(v, key, value);
});
return obj;
};
function fixup<V, T extends object, K extends string = string>(obj: T, key: K, value: V) {
return { ...obj, [key]: value } as AppendToObject<T, K, V>;
}
Note 2: the code for IConfigurator
was left out to start and while it will need updating if we find a solution I don't believe it is the root of the problem. None-the-less, I can think of no reason anyone should believe me on this.
interface IConfigurator<C> {
set<V, K extends string, KV = { [U in K]: V }>(key: K, value: V): IConfigurator<C & KV>;
done(): C;
}