• 请不要在回答技术问题时复制粘贴 AI 生成的内容
tlerbao
V2EX  ›  程序员

2024 年 Interface OR Type 如何选择?

  •  
  •   tlerbao · Feb 20, 2024 · 2938 views
    This topic created in 987 days ago, the information mentioned may be changed or developed.

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

    Google 搜索第一条

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

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

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

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

    8 replies  •  2024-02-21 10:09:07 +08:00
    sch1111878
    sch1111878
    Feb 20, 2024
    1
    如果只是定义数据对象声明, 就 type

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

    也刻意去搜过, 包括官方的问题, 并没有看出特别明显的内容, 随主流就好
    huijiewei
    huijiewei
    Feb 20, 2024
    2
    type 和 interface 不精通很难确定用哪个。所以我全用 type 了。
    Leviathann
    Leviathann
    Feb 20, 2024
    3
    听说 interface 是 lazy 的,大代码库里性能会更好
    SayHelloHi
    SayHelloHi
    Feb 20, 2024
    4
    有时候 type 一把梭 有时候 interface 一把梭
    songray
    songray
    Feb 20, 2024
    5
    简单类型/业务类型 => interface
    类型体操 => type
    extends > &
    caisanli
    caisanli
    Feb 20, 2024
    6
    type 一把梭,少写几个字母(/doge
    Amyang
    Amyang
    Feb 20, 2024
    7
    这文章里说的“But the TS team recommends you default to using interface and only use type when you need to.”
    tlerbao
    tlerbao
    OP
    Feb 21, 2024
    8
    @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

    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4257 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 01:08 · PVG 09:08 · LAX 17:08 · JFK 20:08
    ♥ Do have faith in what you're doing.