Typescript 类型脚本可扩展类型定义

Typescript 类型脚本可扩展类型定义,typescript,Typescript,我有基本类型: interface BaseBlockProps { dispatch?: PageMakerDispatch; ctx?: ServiceContext; formValues?: FieldValues; [propName: string]: unknown; // <-- here is the problem } TypeScript编译现在失败,错误为: TS2344: Type 'TitleAndSubtitleProps' does not

我有基本类型:

interface BaseBlockProps {
  dispatch?: PageMakerDispatch;
  ctx?: ServiceContext;
  formValues?: FieldValues;
  [propName: string]: unknown; // <-- here is the problem
}
TypeScript编译现在失败,错误为:

TS2344: Type 'TitleAndSubtitleProps' does not satisfy the constraint 'BaseBlockProps'.   Index signature is missing in type 'MobileTitleAndSubtitleProps'.

找到了一个解释,即期望的行为对于接口是不可能的,但对于类型是有效的。

如果要扩展基类型,可能根本不需要占位符
[propName:string]:未知的
属性--只需在扩展接口中显式添加所需的属性即可。例如:

interface BaseBlockProps{
调度?:PageMakerDispatch;
ctx?:服务上下文;
formValues?:字段值;
}
导出接口标题子标题BlockProps扩展BaseBlockProps{
标题文本:字符串
字幕文本:字符串
}

如您所见,.

您能添加一个可复制的示例吗?其思想是将派生类型的所有自定义道具组合到该记录中
TS2344: Type 'TitleAndSubtitleProps' does not satisfy the constraint 'BaseBlockProps'.   Index signature is missing in type 'MobileTitleAndSubtitleProps'.