Error TS2339: Property 'customProperty' does not exist on type 'Window & typeof globalThis'

Discussão em 'Angular' iniciado por Kelvin, Dezembro 23, 2024.

  1. Kelvin

    Kelvin Guest

    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?

    Continue reading...

Compartilhe esta Página