【问题标题】:What the generic name of named tuple in Typescript?Typescript中命名元组的通用名称是什么?
【发布时间】:2021-06-02 01:32:10
【问题描述】:

我写了这段代码:

type SomeOptions = {
  a: string;
};

type SomeMethod<T extends any[]> = (
  ...args: [...T, otherOptions: SomeOptions]
) => void;

但 TypeScript 检查器会抛出错误:“元组成员必须都具有名称或都没有名称。”

那么如何将T 定义为命名元组?

【问题讨论】:

    标签: typescript typescript-typings typescript-generics


    【解决方案1】:

    这可能是你想要的:

    type SomeOptions = {
      a: string;
    };
    
    type SomeMethod<T extends any[]> = (
      ...args: [...pre: T, otherOptions: SomeOptions]
    ) => void;
    

    type SomeOptions = {
      a: string;
    };
    
    type SomeMethod<T extends any[]> = (
      ...args: [...T, SomeOptions]
    ) => void;
    

    【讨论】:

    • 不,我正确理解了错误文本。我希望所有参数都被命名。
    猜你喜欢
    • 2020-12-17
    • 2011-02-27
    • 2014-12-16
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2017-05-29
    相关资源
    最近更新 更多