Illustrate with an example:
class ModuleOptions {
key1?: string;
key2?: string;
keyA?: string;
keyB?: string;
}
class Module {
static options: ModuleOptions = {
key1: 'key1',
key2: 'key2',
keyA: 'keyA',
keyB: 'keyB'
};
static create(options: ModuleOptions) {
Object.assign(Module.options, options);
}
}
const myModule = Module.create({
key1: 'foo'
});
// Now Module.options static field has "foo" as key1...
// Can we enforce that fooArgs must have a property named "foo" of type string?
function foo(fooArgs: NewType) {
// Do stuff
}
Is it possible to restrict fooArgs
(represented by NewType
) to only accept 'foo'
as a key, along with the same defined type for it (string
)?
The following attempt does not work (even in a simplified form):
class NewType {
[k in keyof [ModuleOptions.options]]: string;
}
A computed property name must be of type 'string', 'number', 'symbol', or 'any'.