Given an interface
interface Foo {
value1: boolean;
value2: boolean;
}
how may I create an object based on this interface that has all its value set to true
(or false)?
Something that might illustrate what I mean:
const object: Foo = { [keyof Foo]: true }
Any help would be much appreciated.
CodePudding user response:
We should create a class that implements
the interface and use it to construct new objects:
class FooImpl implements Foo {
constructor(value1: boolean, value2: boolean) {
this.value1 = value1;
this.value2 = value2;
}
}
and later use it:
const F = new FooImpl(true, true);
CodePudding user response:
Do the opposite and derive Foo
from your variable:
const object = {
value1: true,
value2: true,
};
type Foo = typeof object;
If you need an interface (for its marginal differences in behavior vs type
) you could do something like this:
type _Foo = typeof object;
interface Foo extends _Foo {}