[求助] 类型 { K:V } --> { "type": K , "conf": V } 且约束 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lqzhgood
V2EX    TypeScript

[求助] 类型 { K:V } --> { "type": K , "conf": V } 且约束

  •  1
     
  •   lqzhgood 2023-10-13 13:05:44 +08:00 1608 次点击
    这是一个创建于 794 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题

    enum Tpe { Title = 'Title', Input = 'Input', } type InputCOnfType= number; type TitleCOnfType= string // map 1 对 1 type COnfList= { [Type.Input]: InputConfType, [Type.Title]: TitleConfType, }; // 想生成一个接口 A, 确保 type 和 conf 获得对应类型, 适用于以下情况 const Input : A ={ type: Type.Input, conf: 11 } const Title : A ={ type: Type.Title, conf: 'str' } // 如果出现有以下情况则报错 const ErrorTemplate : A ={ type: Type.Input, conf: 'str } 

    想象中的 A 接口 效果 类似于, 求解....

    interface A < T=ConfList, K=keyof T > { type: K; conf: T[K] } 

    吐槽

    Ts 想写完美, 特别是涉及 约束 真像是戴着镣铐跳舞....
    as 一把梭又感觉不太优雅~~~

    4 条回复    2023-10-14 10:36:35 +08:00
    lqzhgood
        2
    lqzhgood  
    OP
       2023-10-14 00:45:58 +08:00
    @Opportunity #1

    大佬 NB, 完全没想到可以用辅助类型

    我继续简化了一下

    ```ts
    // # 1
    interface _A<T extends Type> {
    type: T;
    conf: ConfList[T];
    }

    // # 2
    type A <U = Type> = (U extends Type ? _A<U> : never)

    ```

    但是还有个疑问

    #1 的作用 根据传入的泛型返回约束的类型, 返回结果等于
    { type: Type.Input, conf: InputConfType } | { type: Type.Title, conf: TitleConfType}

    #2 如果我写成 type A = _A<Type> 为啥就不行呢 ? 这不是和 #2 等价么...
    YuJianrong
        3
    YuJianrong  
       2023-10-14 03:22:02 +08:00   1
    这是 Conditional Types( 就是这个:x extends TypeA ? A : B )的特性。
    做 Conditional Types 的时候,Typescript 编译器会把 union 分开一个个 map 去算,然后再合成一个 union 。所以

    A<Type>
    相当于
    A<Type.Title | Type.Input>
    相当于
    (Type.Title extends Type ? _A<Type.Title> : never) | ( Type.Input extends Type ? _A<Type.Input> : never)

    你直接 _A<Type> 当然就没有这个效果了。

    文档: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
    lqzhgood
        4
    lqzhgood  
    OP
       2023-10-14 10:36:35 +08:00
    @YuJianrong 受教了 谢谢~
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3872 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 05:22 PVG 13:22 LAX 21:22 JFK 00:22
    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