2024 年 Interface OR Type 如何选择?

79 天前
 tlerbao

前端菜一枚 关于 typescript Interface OR Type 如何选择?

Google 搜索第一条

https://www.totaltypescript.com/type-vs-interface-which-should-you-use

英文菜,大致结论是 默认建议全部使用 type ,只在需要(继承)的时候使用 interface 。

type 虽然可以 & ,但笔者说效率弱于 extends 。

不知道大家 interface 和 type 是怎么选择的?

1421 次点击
所在节点    程序员
8 条回复
sch1111878
79 天前
如果只是定义数据对象声明, 就 type

我原来是用 interface 的, 但是看 ant-design-pro(umi) 的项目都是用的 type, 我现在也改用 type 了

也刻意去搜过, 包括官方的问题, 并没有看出特别明显的内容, 随主流就好
huijiewei
79 天前
type 和 interface 不精通很难确定用哪个。所以我全用 type 了。
Leviathann
79 天前
听说 interface 是 lazy 的,大代码库里性能会更好
SayHelloHi
79 天前
有时候 type 一把梭 有时候 interface 一把梭
songray
79 天前
简单类型/业务类型 => interface
类型体操 => type
extends > &
caisanli
78 天前
type 一把梭,少写几个字母(/doge
Amyang
78 天前
这文章里说的“But the TS team recommends you default to using interface and only use type when you need to.”
tlerbao
78 天前
@Amyang 但是他又说:I'd like to recommend the opposite. The features of declaration merging and implicit index signatures are surprising enough that they should scare you off using interfaces by default.

并且这两天我搜索的结果看,还是推荐 type 的多,包括 ant design pro 团队
https://pro.ant.design/zh-CN/docs/type-script

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1016741

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX