jahnsli

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

  •  
  •   jahnsli · Oct 2, 2022 · 2215 views
    This topic created in 1512 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
    noe132
    Oct 2, 2022
    1
    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
    jahnsli
    OP
    Oct 4, 2022
    2
    但是 params 是 unknown 类型,导致第二个参数无法正确提示
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5186 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 02:22 · PVG 10:22 · LAX 18:22 · JFK 21:22
    ♥ Do have faith in what you're doing.