打字稿省略接受更广泛的类型

发布于 2025-01-28 17:48:18 字数 427 浏览 2 评论 0原文

打字稿允许以下内容,想知道这是否是设计?我以为不应该允许它返回 a “更宽” point在一个想要返回boint< point的函数中,'whyther'>

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)

游乐场链接

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文