V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
tlerbao
V2EX  ›  程序员

2024 年 Interface OR Type 如何选择?

  •  
  •   tlerbao · Feb 20, 2024 · 2313 views
    This topic created in 809 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
        1
    sch1111878  
       Feb 20, 2024
    如果只是定义数据对象声明, 就 type

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

    也刻意去搜过, 包括官方的问题, 并没有看出特别明显的内容, 随主流就好
    huijiewei
        2
    huijiewei  
       Feb 20, 2024
    type 和 interface 不精通很难确定用哪个。所以我全用 type 了。
    Leviathann
        3
    Leviathann  
       Feb 20, 2024
    听说 interface 是 lazy 的,大代码库里性能会更好
    SayHelloHi
        4
    SayHelloHi  
       Feb 20, 2024
    有时候 type 一把梭 有时候 interface 一把梭
    songray
        5
    songray  
       Feb 20, 2024
    简单类型/业务类型 => interface
    类型体操 => type
    extends > &
    caisanli
        6
    caisanli  
       Feb 20, 2024
    type 一把梭,少写几个字母(/doge
    Amyang
        7
    Amyang  
       Feb 20, 2024
    这文章里说的“But the TS team recommends you default to using interface and only use type when you need to.”
    tlerbao
        8
    tlerbao  
    OP
       Feb 21, 2024
    @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   ·   Solana   ·   2505 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 00:30 · PVG 08:30 · LAX 17:30 · JFK 20:30
    ♥ Do have faith in what you're doing.