TypeScript does not see all possible values of a type [duplicate]

6 days ago 7
ARTICLE AD BOX

I have an issue where TypeScript does not consider that async function may change an object, and it only sees the most recent assignment:

type A = { status: "error" | "ok" | "new" | "pending" } const obj: A = { status: "new" } obj.status = "pending"; await functionThatCanChangeObj();

obj.status is seen as "pending" here by TypeScript even though it can be changed by the async function above. If I do a check like if(obj.status === "error") the TypeScript gives error:

This comparison appears to be unintentional because the types '"error"' and '"pending"' have no overlap

It works if I do an assertion in the assignment, but I don't get why TypeScript does this.

Read Entire Article