1

Here is my component:

import {Button, Tabs} from 'antd';
import * as React from "react";
import {ReactElement} from "react";
import {IBaseComponentProps} from "../BaseComponent/BaseComponent";

const operations = <Button type={"primary"}>Extra Action</Button>;

export interface ITabContent {
  title: string,
  content: ReactElement<any>
}

interface ITabProps extends IBaseComponentProps {
  tabs: ITabContent[]
}

export const Tab: React.SFC<ITabProps> = props => {
  const tabs: any = [];
  props.tabs.forEach((tab: ITabContent, index: number) => {
    tabs.push(<Tabs.TabPane tab={tab.title} key={index}>{tab.content}   </Tabs.TabPane>)
  });
  return (
      <Tabs tabBarExtraContent={operations}>
        {...tabs}
      </Tabs>
  );
};

I want to force tabs in my props to not be empty, preferably I would like a compile time error.

I know you can do something like a "OR" with type inference in typescript:

boolean = "false" | "true"

But I can't seem to figure out a way to type tabs correctly. For example, I tried something like :

tabs: ITabContent[] & !never[]

But this obviously doesn't compile. Is there an equivalent operator to "&" that I am not aware of?

1
  • What do you mean by "I want to force tabs in my props to not be empty" ? you can specify the allowed type as tabs: ITabContent[] it will only allow array with those properties Commented May 15, 2018 at 15:17

1 Answer 1

1

Non-empty array can be forced with:

interface ITabContentArray extends Array<ITabContent> {
  0: ITabContent;
  [n: number]: ITabContent;
}

interface ITabProps extends IBaseComponentProps {
  tabs: ITabContentArray;
}

Or:

interface ITabProps extends IBaseComponentProps {
  tabs: ITabContent[] & { 0: ITabContent };
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.