You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript带索引签名的泛型接口赋值报错问题求助

关于TypeScript泛型接口赋值错误的问题

我定义了这样一个TypeScript接口,它允许任意属性名(通过other索引签名实现),同时还提供了几个常用属性的类型提示:

export interface IGenericChannelSuggestion { 
  emailText?: string; 
  emailHtml?: string; 
  sms?: string; 
  [other: string]: string; 
}

之后我又定义了一个泛型类,其中泛型参数O作为generate()方法的返回类型,默认回退到上面的IGenericChannelSuggestion接口:

class TypedTemplate<T = IDictionary, O = IGenericChannelSuggestion> { ... }

但在实现generate()方法的泛型类型逻辑时,初始化输出的时候碰到了错误:Typed property with index type can not be assigned value。而且这个接口本身并没有必填属性,后续的测试代码还出现了更奇怪的问题,我现在完全搞不清报错的根源,也不知道到底哪些值可以赋值给类型O。

内容的提问来源于stack exchange,提问作者ken

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:03:44