执行ng serve遇TS2610继承覆盖错误,求解决方案
问题场景
执行ng serve启动项目时触发TS2610类型错误,具体错误信息如下:
X [ERROR] TS2610: 'sticky' is defined as an accessor in class 'CdkColumnDef', but is overridden here in 'NbColumnDefDirective' as an instance property. [plugin angular-compiler]
node_modules/@nebular/theme/components/cdk/table/cell.d.ts:38:4: 38 │ sticky: boolean; ╵ ~~~~~~
X [ERROR] TS2610: 'sticky' is defined as an accessor in class 'CdkHeaderRowDef', but is overridden here in 'NbHeaderRowDefDirective' as an instance property. [plugin angular-compiler]
node_modules/@nebular/theme/components/cdk/table/row.d.ts:29:4: 29 │ sticky: boolean; ╵ ~~~~~~
X [ERROR] TS2610: 'sticky' is defined as an accessor in class 'CdkFooterRowDef', but is overridden here in 'NbFooterRowDefDirective' as an instance property. [plugin angular-compiler]
node_modules/@nebular/theme/components/cdk/table/row.d.ts:39:4: 39 │ sticky: boolean; ╵ ~~~~~~
已尝试升级typescript-plugin-rxjs至5.0.3、@angular/cdk和@angular/core至17.2.2,但问题仍未解决。
解决方案
1. 升级@nebular.theme到兼容Angular 17的版本
错误根源是Angular CDK 17将sticky属性改为访问器(getter/setter),但旧版@nebular/theme仍用实例属性重写,导致类型冲突。升级@nebular/theme到适配Angular 17的版本即可解决:
npm install @nebular/theme@latest # 若使用yarn yarn add @nebular/theme@latest
2. 检查依赖树,确保@angular/cdk版本统一
如果升级后仍报错,执行以下命令查看依赖树,确认没有其他依赖锁定了旧版@angular/cdk:
npm ls @angular/cdk
若存在版本不一致,可通过npm dedupe或者手动调整依赖版本解决。
3. 临时绕过类型检查(不推荐长期使用)
若暂时无法升级@nebular/theme,可在tsconfig.json的compilerOptions中添加以下配置,跳过第三方库的类型检查:
"skipLibCheck": true
此方法能临时启动项目,但可能隐藏其他潜在类型问题,仅作为临时方案。
内容的提问来源于stack exchange,提问作者Abdullah Afnan

