why does typescript not discriminate a base type from its intersection types

65 Views Asked by At

This question might already be answered somewhere in this somewhat similar question, however the example code and explanations are a very verbose and I can't really seem to distill it down the useful parts.

I am hoping someone can provide an explanation as to why the following code doesn't produce a type error on the indicated line. Is there any utility type I can use which would produce the result I want?

Playground

type BaseMessage = { foo: string }

type JobMessage = BaseMessage & { bar: number }

const processBase = (msg: BaseMessage) => {}

const processJob = (msg: JobMessage) => {
  // I expect this next line to error
  processBase(msg);
  // This produces the error I am hoping for
  processBase({ foo: 'str', bar: 1234 });
}
0

There are 0 best solutions below