some-electrician-52054
01/26/2023, 1:24 PMERROR in src/app/xxx....xxx.service.ts:1471:37 - error TS2339: Property 'remove' does not exist on type 'MyClass[]'.
I configured the typeRoots in my tsconfig.json to tell TS about my extension method remove for arrays:
json
"typeRoots": [
"node_modules/@types",
"app/extensions/@types" <--- contains remove
],
Has anybody had the same issue? Is a configuration missing? As I mentioned before, these errors don't effect the functionality of the test run, but it's a bit annoying to have hundreds of them in the output.orange-baker-20977
01/26/2023, 4:20 PMsome-electrician-52054
01/26/2023, 4:22 PMorange-baker-20977
01/26/2023, 4:32 PM**.service.ts from your tsconfig?some-electrician-52054
01/26/2023, 4:35 PMorange-baker-20977
01/26/2023, 5:38 PMsome-electrician-52054
01/27/2023, 11:12 AMtypescript
export function addExtensions(): void {
if (!Array.prototype.single) {
Array.prototype.single = function () {
if (this.length === 0) {
throw new Error("array contains no matching elements");
}
if (this.length > 1) {
throw new Error("array contains more than one element");
}
return this[0];
};
}
}
- add /src/app/extensions/types/extensions.d.ts
typescript
interface Array<T> {
single(): T;
}
- call addExtensions() in component.ts
typescript
import { addExtensions } from '../../src/app/extensions/extensions';
addExtensions();
- create new component "DemoComponent"
add public field to class src/app/demo/demo.component.ts that uses extension method
typescript
export class DemoComponent {
public value = [1].single();
}
- Set html-template to
Value: {{ value }}
- create test app/demo/demo.component.cy.ts
typescript
import { DemoComponent } from "./demo.component";
describe("DemoComponent", () => {
it(`should create`, () => {
cy.mount(DemoComponent);
});
});
- run ng e2e --component
Result:
ā Compiled successfully.
asset cypress-support-file.js 8.1 MiB {cypress-support-file} [emitted] (name: cypress-support-file)
...
chunk {cypress-entry} (runtime: runtime) cypress-entry.js (cypress-entry) 196 KiB [initial]
9 public value = [1].single();
~~~~~~
ERROR in src/app/extensions/extensions.ts:2:26 - error TS2339: Property 'single' does not exist on type 'any[]'.
2 if (!Array.prototype.single) {
~~~~~~
The app compiles and the test runs successfully.some-electrician-52054
01/27/2023, 12:33 PMtypescript
...
interface Array<T> {
single(): T;
}
...
but that produces some warnings that seem to make sense since this is a component test:
chunk {styles} (runtime: runtime) styles.css, styles.js (styles) 194 KiB (javascript) 79 bytes (css/mini-extract) [initial]
WARNING in C:/Projekte/cypress-bug/cypress/e2e/spec.cy.ts is part of the TypeScript compilation but it's unused.
Add only entry points to the 'files' or 'include' properties in your tsconfig.
WARNING in C:/Projekte/cypress-bug/src/app/app.component.ts is part of the TypeScript compilation but it's unused.
Add only entry points to the 'files' or 'include' properties in your tsconfig.
WARNING in C:/Projekte/cypress-bug/src/app/app.module.ts is part of the TypeScript compilation but it's unused.
Add only entry points to the 'files' or 'include' properties in your tsconfig.
WARNING in C:/Projekte/cypress-bug/src/main.ts is part of the TypeScript compilation but it's unused.
Add only entry points to the 'files' or 'include' properties in your tsconfig.
Not sure if that helps...