V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
jahnsli
V2EX  ›  TypeScript

typescripr 中,如何写能随意组合函数参数并且能得到对应类型的提示

  •  
  •   jahnsli · 2022 年 10 月 2 日 · 1564 次点击
    这是一个创建于 1202 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如何定义一个函数有 4 个参数(一个必选,三个可选),能随意组合参数并且能得到对应类型的提示

    function name ( url,params:type ){}
    function name ( url,params:type,config ){}
    function name ( url,params:type,config,option ){}
    
    
    

    用的时候

    name('http',{params}) 或
    name('http',{config})  或 
    name('http',{option })   或
    name('http',{params}, {option }) 或
    name('http',{config}, {option })
    
    noe132
        1
    noe132  
       2022 年 10 月 2 日
    interface Fn {
    (a: string) => string
    (a: string, b: boolean) => string
    (a: string, b: boolean, c: number) => number
    }

    const fn: FN = (a: string, b?: boolean, c?: number) => {
    // TODO:
    }
    jahnsli
        2
    jahnsli  
    OP
       2022 年 10 月 4 日
    但是 params 是 unknown 类型,导致第二个参数无法正确提示
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   2854 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:14 · PVG 17:14 · LAX 01:14 · JFK 04:14
    ♥ Do have faith in what you're doing.