2024 年 Interface OR Type 如何选择? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
请不要在回答技术问题时复制粘贴 AI 生成的内容
tlerbao

2024 年 Interface OR Type 如何选择?

  •  
  •   tlerbao Feb 20, 2024 2269 views
    This topic created in 796 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     915 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 44ms UTC 22:35 PVG 06:35 LAX 15:35 JFK 18:35
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86