打字稿省略接受更广泛的类型
打字稿允许以下内容,想知道这是否是设计?我以为不应该允许它返回 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论