jahnsli

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

  •  
  •   jahnsli · Oct 2, 2022 · 1726 views
    This topic created in 1321 days ago, the information mentioned may be changed or developed.

    如何定义一个函数有 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  
       Oct 2, 2022
    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
       Oct 4, 2022
    但是 params 是 unknown 类型,导致第二个参数无法正确提示
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2947 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 14:11 · PVG 22:11 · LAX 07:11 · JFK 10:11
    ♥ Do have faith in what you're doing.