TypeScript function accepts larger type, that it is required

Viewed 44

I was testing out functions, when I discovered intresting behevior. For some reason function accepts a "Larger" type, when "Smaller" is required.

  • Why?
  • And how to make it strict?

type Larger = {
  prop1: string,
  prop2: string,
};

type Smaller = {
  prop1: string;
}

const foo = (param: Smaller): Smaller => param;

const larger: Larger = {
  prop1: 'prop1',
  prop2: 'prop2'
};
const result: Smaller = foo(larger);

0 Answers
Related