'Typescript false negative on extra key with complex union types [duplicate]

I have the following minimal viable example:

export type Left<E> = {
    readonly _tag: 'Left'
    readonly left: E
}

export type Right<A> = {
    readonly _tag: 'Right'
    readonly right: A
}

export type Either<E, A> = Left<E> | Right<A>

export const left = <E = never, A = never>(e: E): Either<E, A> => ({_tag: 'Left', left: e})

type A = Either<{type: 1}, {type: 2}>

const f = (a: A): void => {}

f(left({type: 1, extraKey: 2})) // Expect this to throw a type error because of the extra key, but it does not on TS 4.6.3

The Either type is strongly inspired from the Either type in the fp-ts library.

I expected the last line to throw a type error because of the presence of the extra key, but it does not on TypeScript 4.6.3.

Is this a bug, or a feature?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source