What is the best way to utilize the fresh Sanitizer API in Typescript?

Everything seems to be working well on Codepen, even without using window. It's surprising because I'm used to having to use window.x

if ( 'Sanitizer' in window ) {
    console.log( 'sani', 'Sanitizer' in window );
}

const c = new Sanitizer();
console.log( c );

const sa = ( window as any ).Sanitizer;
console.log( 'sa', sa );

I have everything set up and the works perfectly. I've tried every possible way, but my compiled script doesn't seem to recognize the Sanitizer API. I've tried calling it in various ways, but nothing seems to work. Do I need to configure something in the tsconfig? What's happening?

export {};
declare global {
    interface Window {
        works: inlineScriptJSON;
        Sanitizer?: any;
    }
}

    console.log(
        'window.Sanitizer',
        window.Sanitizer,
        // 'new window.Sanitizer()',
        // new window.Sanitizer()
    );

window.Sanitizer is showing as undefined.

new window.Sanitizer() is giving this error:

Uncaught TypeError: window.Sanitizer is not a constructor

In my tsconfig.base.json, the values under "lib" are underlined and it mentions that they are not acceptable options. I know this is new, but it works in Codepen, so what's different?

{

    /* https://github.com/WordPress/gutenberg/blob/trunk/tsconfig.base.json */
    "compileOnSave": false,
    "compilerOptions": {
        "allowJs": true,
        "checkJs": true,
        "allowSyntheticDefaultImports": true, // to make it compatible with babel
        "jsx": "preserve",
        "target": "esnext",
        "module": "esnext",
        "lib": [
            "esnext",
            "DOM",
            "DOM.Iterable",
            "DOM.Sanitizer",
            "Sanitizer",
        ],
        "declaration": true,
        "declarationMap": true,
        "composite": true,
        "emitDeclarationOnly": true,
        "isolatedModules": true,

        /* Strict Type-Checking Options */
        "strict": true,

        /* Additional Checks */
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "importsNotUsedAsValues": "error",

        /* Module Resolution Options */
        "moduleResolution": "node", // because of webpack

        /* This needs to be false so our types are possible to consume without setting this */
        "esModuleInterop": false,
        "resolveJsonModule": true,

        "typeRoots": [ "./node_modules/@types" ],
        "types": [],
        
        /* nico */
        "strictNullChecks": true,
        "noImplicitAny": false,
        "removeComments": true,
        "allowUnreachableCode": false,
        "sourceMap": true,
    },
    "exclude": [
        "**/build/**",
        "**/test/**"
    ],
}


Answer №1

It all makes sense now. Darn it. The reason is clear - this API functions solely in a HTTPS environment, while my local development site lacks HTTPS. Consequently, the API endpoint is essentially non-existent in this scenario, completely unrelated to typescript.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is a Generic Type in an Array?

Currently, I'm working on designing a mockup app that displays data in the form of buttons in a list. I have successfully implemented most of the features but I have encountered a problem. I have initialized an array and another class to manage the da ...

Mismatch in TypeScript React Intl Redux form types encountered

Currently, I am facing an issue trying to connect my form to Intl and struggling with a TypeScript error. The error seems to disappear when I change injectIntl<any>, but then I'm not sure what exactly needs to be passed there. Can someone please ...

Input values that are true, or in other words, fulfill conditions for truthiness

Is there a specific data type in TypeScript to represent truthy values? Here's the method I'm working with: Object.keys(lck.lockholders).length; enqueue(k: any, obj?: any): void It seems like TypeScript allows checking for empty strings &ap ...

Encountering an error with TypeScript generic parameters: Expecting '?' but receiving an error message

As a newcomer to TypeScript, I am currently exploring the use of generic type parameters. I have encountered an error message: '?' expected while working on a function and a type that both require type arguments. The issue seems to be in the Inpu ...

Struggling to transmit data to material dialog in Angular2+

I am facing an issue with my Material Dialog not working properly. Can anyone point out what I might be missing? product-thumbnail.ts I will use this to trigger the dialog export class ProductThumbnailComponent implements OnInit { @Input() product: Pr ...

Encountering a problem when launching the "vite-express" template on the Remix app (TSConfckParseError: error resolving "extends")

I recently launched a brand-new [email protected] project using the remix-run/remix/templates/vite-express template after executing this command: npx create-remix@latest --template remix-run/remix/templates/vite-express However, upon trying to run th ...

When the keyboard appears, the Ionic 2 form smoothly slides upwards

I am currently working with the most recent version of Ionic 2. In my code, I have a <ion-content padding><form></form></ion-content> containing a text input. However, when attempting to enter text on an Android device, the keyboard ...

Switch up your component button in Angular across various pages

I've created a new feature within a component that includes a toolbar with multiple buttons. Is there a way to customize these buttons for different pages where the component is used? Component name <app-toolbar></app-toolbar> Component ...

Innovative approaches to enhancing Feathers services through the use of relational data in design patterns

I'm in the process of developing a straightforward application that involves a one-to-many relationship between data entities. Specifically, I am working with feathers js and sequelize (utilizing sqlite) to create a system where each site can have mul ...

Changing the function to operate asynchronously

How can I convert the following code into an asynchronous function? It is currently returning referralUrl as undefined: controller async createReferralUrls() { this.referralUrl = await this.referralService.generateReferralUrl(this.userData.referral ...

What is the best way to fetch data before a component is rendered on the screen?

I am facing an issue with fetching data from a local server in Node. When I try to render the component, the array 'users' from the state appears to be empty, resulting in no users being displayed on the screen as intended. What's strange is ...

No data being displayed or returned from API when using async await

My Ionic 6 + Angular 14 application is currently facing an issue with displaying data retrieved from an API... I have implemented a service to fetch the data from the API and then called this service in the component. The app compiles without any errors a ...

Adjust the color of an SVG icon depending on its 'liked' status

In my React/TypeScript app, I have implemented an Upvote component that allows users to upvote a post or remove their upvote. The icon used for the upvote is sourced from the Grommet-Icons section of the react-icons package. When a user clicks on the icon ...

Struggling with configuring internationalization in NestJS

Currently, I am working on a NestJS project where my lead assigned me the task of returning different errors to the frontend based on the language in the request header. After some research, I decided to use i18n for this purpose. However, when testing it ...

Exploring the Differences between Angular's Http Module and the Fetch API

While I grasp the process Angular uses for HTTP requests, I find myself leaning towards utilizing the Fetch API instead. It eliminates the need to subscribe and unsubscribe just for a single request, making it more straightforward. When I integrated it int ...

Employing async await for postponing the execution of a code block

I have been working on an Angular component where I need to perform some actions after a data service method returns some data asynchronously. Although I attempted to use async/await in my code, I feel that I may not have fully understood how it works. Her ...

Accessing HTTP data through a function instead of using ngOnInit in Angular is a more efficient approach

Trying to retrieve data from a service using setInterval has posed an issue for me. When I call the service from ngOnInit, everything functions as expected. However, when attempting to call it from any other function, an error occurs: "ERROR TypeError: Ca ...

Should one prioritize learning TypeScript over diving into Angular2?

Should I prioritize learning TypeScript before diving into AngularJS 2? ...

Ways in which this data can be best retrieved

Hey there, I'm currently in the process of building an Ionic2 app with Firebase integration. Within my codebase, there's a provider known as Students-services where I've written a function to navigate through a node, retrieve values, and dis ...

Building a filter for a union type in TypeScript: a step-by-step guide

Allow me to present an example to demonstrate my current objective. const v1: { type: "S"; payload: string } = { type: "S", payload: "test" }; const v2: { type: "N"; payload: number } = { type: "N", payload: 123 }; type Actions = typeof v1 | typeof v2; ...