I encountered Error TS2339: Property 'customFunction' does not exist on type 'Window & typeof globalThis'.
when trying to build my Angular library.
I am trying to add .d.ts file in my freshly generated library and still encountered the issue during build. I tried the solution from multiple questions here, and yet I still encountered the issue.
Here my steps to replicate:
First I generated new workspace:
ng new test-workspace --create-application=false
Then, inside the workspace, I generate a new Angular Library
cd test-workspace
ng generate library lib-test
After that, I generated a directory in lib-test under lib-test/src/types.
Then, I created a file window.d.ts with following content
declare global {
interface Window {
customFunction: {
startService: (arg: string) => void;
endService: (arg: string) => void;
}
}
}
export {};
After that, I called window with the custom property in the lib-test.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class LibTestService {
constructor() {
window.customFunction.startService("test");
}
}
On VSCode, there aren't any error Property 'customFunction' does not exist on type 'Window & typeof globalThis'.
. But during ng build lib-test, I encountered this error.
Can someone help to point out am I missing any steps for implementing this customFunction?
Try adding types to the tsconfig.
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"types": ["./src/types/index.d.ts"]
},
"exclude": ["**/*.spec.ts"]
}
npm i
-> cd test-workspace
-> npm i
-> cd projects/lib-test
-> npm i
-> ng build lib-test
The fix for the issue is to add the reference to the type using the triple slash include
syntax.
/*
* Public API Surface of lib-test
*/
/// <reference path="./types/index.d.ts" />
export * from './lib/lib-test.service';
export * from './lib/lib-test.component';
After doing this the build was successful, The below github issue helped me find the solution (Comment from dherges.
npm i
-> cd test-workspace
-> npm i
-> cd projects/lib-test
-> npm i
-> ng build lib-test