1

Below is my code

const url: any = new URL((global as any).window.location);

However, typescript says can not find name URL.

How can I fix it?

1 Answer 1

2

Short version: The URL type is declared in the "DOM" lib. Add that to the "lib" section of your tsconfig.json:

{
    "compilerOptions": {
        "lib": ["dom"],
    }
}

(if you already have other entries under "lib", that's fine too - just add "dom" to the array)

Long version:

The "lib" compiler option indicates which groups of built-in type definitions TypeScript should include. For example, you may include the "es2015" group to include type definitions for features added in ES2015.

The "dom" lib option refers to types that exist in browser environments but not Node environments. The URL class is an example of that, so it's declared in the "dom" types.

Note that TypeScript includes the "dom" types by default. I'm guessing you've overridden the list to something else, and aren't including them in yours.

For more info, see:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.