This Options
class has a factory function that returns a custom instance of the class. This is it:
export class Options {
quadrant?: 1 | 2 | 3 | 4 = 1;
static factory(options: Partial<Options>): Options {
const defaults: Options = new Options();
return {
...defaults,
...options,
};
}
}
And I’m trying to pass in an factory function argument that modifies the quadrant
like this:
export const defaults = {
quadrant: 2,
};
const responsive: Options = Options.factory({
...defaults,
});
And it produces this error:
Argument of type '{ quadrant: number; }' is not assignable to parameter of type 'Partial<Options>'.
Types of property 'quadrant' are incompatible.
Type 'number' is not assignable to type '1 | 2 | 3 | 4'.(2345)
Is there way to fix this?
2
Answers
You can use a
const
assertion when initializingdefaults
so that the type checker infers a more specific type for it: in particular, the type of thequadrant
property will be inferred as the literal type2
instead of the widernumber
type:Which is enough to make your
Options.factory()
call work as expected:Playground link to code
If you hover over the
defaults
, you will see that the inferred type isnumber
– you’ll see something like this:There are 2 ways to deal with this issue. You can either cast the defaults
as const
, meaning the type will be as follows:The other way you can achieve what you want is actually type the
defaults
object so it’s not inferred: