More than happy to be informed of a better solution here! But this came up in a real-work situation and I "stumbled" on the solution by more or less guessing.
In plain JavaScript, you have an object which you know you set certain keys on. But because this object is (ab)used for a templating engine, we also put keys/values on it that are not known in advance. In our use case, these keys and booleans came from parsing a .yml
file which. It looks something like this:
const context = {
currentVersion: "3.12",
currentLanguage: "en",
activeDate: someDateObject,
}
if (someCondition()) {
context.hasSomething = true
}
for (const [featureFlag, truth] of Object.entries(parseYamlFile('features.yml')) {
context[featureFlag] = truth
}
const rendered = render(template: { context })
I don't like this design where you "combine" an object with known keys with a spread of unknown keys coming from an external source. But here we are and we have to convert this to TypeScript, the clock's ticking!
Truncated! Read the rest by clicking the link below.