Typescript Omit Accepts Wider Types

125 Views Asked by At

Typescript allows the following, wondering if is this by design? I was thinking it should not be allowed to return a "wider" Point inside a function that wants to return Omit<Point, 'whatever'>.

interface Point {
    x: number,
    y: number
}

function getPoint() {
    return { x: 3, y: 9 }
}

function ohNo(): Omit<Point, 'y'> {
    // is allowing this to compile "by design"?
    return getPoint()
}

const p = ohNo()
console.log('oh no', p)

Playground link here

0

There are 0 best solutions below