使类型接受一个属性+4个集合中的一个



这是我的代码:

interface Props<T> {
config: {
title?: string,
(
limitText: boolean,
bold: boolean,
content: ReactNode
)
| (iterable: boolean, content: T[], selector: string)
| (markup: boolean, content: string)
| (stack: boolean, coloredLevel?: boolean, content: string[])
}
}

我试图实现的是让config接受title,以及其他4种类型中的一种。我怎样才能做到这一点?我认为这是歧视性的工会式协议。

ts游乐场


interface PropsBase<T> {
config: {
title?: string,
}
}
interface Props1 extends PropsBase<unknown> {
config: {
title?: string,
limitText: boolean,
bold: boolean,
content: 'ReactNode'
}
}
interface Props2<T> extends PropsBase<T> {
config: {
title?: string,
markup: boolean,
content: T,
}

}
interface Props3 extends PropsBase<unknown> {
config: {
title?: string
stack: boolean,
coloredLevel?: boolean,
content: string[]
}
}
type Props<T> = Props1 | Props2<T> | Props3;

最新更新