So I was trying to make something unrelated to the problem, and came upon a weird thing in TS. If I create a type for example:
type A = "A" | "B" | "C";
type C = {
variable: A;
}
I can create an object of type C while providing the value of variable
with a string literal "A"
like so:
const OBJ: C = {
variable: "A"
}
But when I create a class implementing C
I have to do "A" as A
, "A" as "C"
or "A" as const
when assigning variable
default value;
class O implements C {
variable = "A"; // throws error `type string is not assignable to type A`
}
Why is that? Is = "A"
inside of classes treated differently?
TS Playground link