0

This works all fine on Codepen, even without window. I am surprised about that because I am used to be forced to use ẁindow.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 this set and works works perfectly fine. I tried everything but by compiled script refuses to know about the Sanitizer API. I tried calling it in all kinds of way, with - nothing. Do i need to set something in tsconfig? WTF is going on?

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

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

window.Sanitizer is undefined.

new window.Sanitizer() gives this error:

Uncaught TypeError: window.Sanitizer is not a constructor

My tsconfig.base.json the values in "lib" are underlined and it says on hover they are not acceptable options. I know this think is new but it works in Codepen so what are they doing?

{

    /* 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/**"
    ],
}


redanimalwar
  • 1,229
  • 1
  • 12
  • 32
  • make sure that the lib option includes the necessary browser APIs and that the target option is set to a compatible version of ECMAScript. – Ashrik Ahamed Mar 24 '23 at 04:45
  • @AshrikAhamed I actually thought about that that why I asked about config. What am I supposed to but in there `DOM.Sanitizer` or just `Sanitizer`just tried both, not working. – redanimalwar Mar 24 '23 at 04:50
  • 1
    That "Uncaught TypeError: window.Sanitizer is not a constructor" is not a typescript error! It simply means your browser does not provide it. Also there is nothing like "DOM.Sanitizer" or "Sanitizer", it is an experimental non standard API. – brc-dd Mar 24 '23 at 04:55
  • @brc-dd I dont want a `d.ts` file and why should that work? I never used such a file I always was able to declare things in the file and the config. And as I have shown my the other variable works. – redanimalwar Mar 24 '23 at 04:56
  • @brc-dd as I told you my Browser *does* provide it, I use Brave and its in Blink and it works in Codepen so typescript refuses it and does not compile it correctly for some reason. I need to look waht is in actual compiled JS – redanimalwar Mar 24 '23 at 05:00

1 Answers1

0

I know why. DAMNIT. This API only works in a HTTPS context and my local dev site is not HTTPS. So its actually really not there and it has nothing to do with typescript.

redanimalwar
  • 1,229
  • 1
  • 12
  • 32