youngluo
V2EX  ›  问与答

ts interface 如何定义一个函数类型 key,其入参类型和返回值类型为实际传入函数对应类型

  •  
  •   youngluo · Jun 5, 2023 · 1350 views
    This topic created in 1104 days ago, the information mentioned may be changed or developed.
    2 replies    2023-06-05 22:55:58 +08:00
    MozzieW
        1
    MozzieW  
       Jun 5, 2023
    也不是说描述不清楚,但是建议加上用例、场景。

    听起来像是:泛型?
    Danswerme
        2
    Danswerme  
       Jun 5, 2023
    这样?

    interface SomeFn<T, R> {
    (params: T): R
    }

    const fn: SomeFn<string, number> = (a) => Number(a);
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2776 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 06:31 · PVG 14:31 · LAX 23:31 · JFK 02:31
    ♥ Do have faith in what you're doing.