简体   繁体   English

TypeScript 中是否有等效的扩展运算符用于接口?

[英]Is there an equivalent of the Spread Operator in TypeScript for use with Interfaces?

I'm working on a JavaScript project and would like to use TypeScript as well.我正在研究 JavaScript 项目,并且也想使用 TypeScript。 In one scenario it would be nice if I could reduce the amount of repetition in this interface:-在一种情况下,如果我可以减少此界面中的重复次数会很好:-

interface Question {
  settings: {
    label: string
    description: string
    hidden: boolean
    followUp: {
      label: string
      description: string
      hidden: boolean
    }
  }
}

If there were some kind of Spread Operator that would be great:如果有某种传播运算符会很棒:

interface QuestionSettings {
  label: string
  description: string
  hidden: boolean
}

interface Question {
  settings: {
    ...QuestionSettings
    followUp: QuestionSettings
  }
}

Is this possible in TypeScript?这在 TypeScript 中是否可行?

You could use intersection types :您可以使用交叉点类型

interface QuestionSettings {
   label: string;
   description: string;
   hidden: boolean;
}

interface Question {
  settings: QuestionSettings & {
     followUp: QuestionSettings;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM