TypeScript报错求助:Property or signature expected等接口语法问题
问题排查:TypeScript接口定义中的"Property or signature expected"等错误
核心原因与修复方案
你的代码错误源于TypeScript接口语法规则的误用:接口仅定义结构契约,不允许使用类的get访问器语法,同时部分方法的格式不符合接口签名规范。以下是修复后的完整代码及逐条错误解释:
修复后的接口代码
import { Countermeasure } from "./classes/Countermeasure"; import { Crash } from "./classes/Crash"; import { Locality, RoadwayType, UnitType } from "./enums"; export interface IAppliedCountermeasure { countermeasure: Countermeasure; unit: UnitType; unitCost: number; quantity: number; } export interface IRoadwayElement { readonly id: number; readonly name: string; readonly locality: Locality; readonly peerGroup: number; readonly roadwayType: RoadwayType; readonly infoProps: Map<string, string | number>; readonly riskFactors: Map<string, boolean | number>; crashes: Crash[]; appliedCountermeasures: IAppliedCountermeasure[]; // 接口中只读属性替代类的getter语法 readonly infoPropsTable: [string, string | number][]; readonly riskFactorsTable: [string, boolean | number][]; addAppliedCountermeasure(acm: IAppliedCountermeasure): void; removeAppliedCountermeasure(index: number): void; }
逐条错误解析
第24行:Property or signature expected
- 错误原因:接口中错误使用了类的
get关键字定义访问器,接口仅允许声明属性/方法签名,不支持类的访问器语法。 - 修复方式:删除
get关键字,将其声明为只读属性readonly infoPropsTable: [string, string | number][];
- 错误原因:接口中错误使用了类的
第24行:';' expected
- 错误原因:错误的
get写法导致TypeScript无法识别正确的语句结构,无法找到预期的分号。 - 修复方式:改为正确的属性签名后,在每行结尾添加分号(TypeScript推荐规范)。
- 错误原因:错误的
第24行:An element access expression should take an argument
- 错误原因:错误的
get写法被TypeScript误判为数组/对象元素访问操作(如get[xxx]),但缺少必要的参数。 - 修复方式:同上述,改为正确的属性签名即可解决。
- 错误原因:错误的
第27行:',' expected
- 错误原因:方法定义中参数列表与返回值之间的多余空格(
(acm: IAppliedCountermeasure) : void)导致解析异常。 - 修复方式:去掉空格,写成
(acm: IAppliedCountermeasure): void;并添加分号。
- 错误原因:方法定义中参数列表与返回值之间的多余空格(
第34行:Expression expected
- 错误原因:前面的语法错误引发连锁解析失败,并非该行本身存在问题。
- 修复方式:修正所有前置语法错误后,该错误会自动消失。
额外注意事项
- TypeScript接口中,所有成员(属性、方法)结尾建议添加分号,避免语法解析歧义。
- 接口只定义结构契约,不需要实现逻辑,因此getter/setter都只需声明为属性,不需要写
get/set关键字。 - 方法签名需保持
(参数列表): 返回类型的紧凑格式,避免参数与返回值之间的多余空格。
内容的提问来源于stack exchange,提问作者Rifat Hasan
相关产品推荐
相关产品推荐

