Giter Site home page Giter Site logo

Comments (8)

probablykasper avatar probablykasper commented on July 18, 2024 1

I think it might be caused by the Svelte VSCode extension, so filed an issue there for now: sveltejs/language-tools#1733

Thanks for the help on figuring it out !! ❤️

from workers-types.

eugene1g avatar eugene1g commented on July 18, 2024

This is hit&miss, but in some cases, it worked for me when to add /// <reference types="@cloudflare/workers-types" /> as the very first line in the TypeScipt file, to help it reference CF types vs lib.dom.

from workers-types.

penalosa avatar penalosa commented on July 18, 2024

It seems like you might be trying to use workers-types in combination with libdom? That's generally not a path we recommend (see the README on npm for the recommended setup). However, with the just-released workers-types@4, you're able to import the types, which may help:

import type { Request as WorkersRequest } from "@cloudflare/workers-types"

from workers-types.

eugene1g avatar eugene1g commented on July 18, 2024

It's probably fine to close this, but the problem is caused by two nuances so I'll leave further notes here -

  1. IDEs often include TypeScript's lib.dom.d.ts for all .ts files even if tsconfig.json has a narrow list of libs like "lib": ["ES2020"]
  2. When the code references global objects like new Request(url), the IDE will use the definition of Request from lib.dom.d.ts that ships with TypeScript, which has incorrect types (for example, the cf parameter is missing). Importing the custom type for Request from @cloudflare/workers-types will not help because the code uses the actual new Request() runtime object which cannot be (reasonably) changed in type via TS type hints.

That's why the workaround is to tell TypeScript to use @cloudflare/workers-types for that file as I showed in the example above. In theory, doing this override in tsconfig.json with the types directive works the same (as is explained in the README), but that didn't do the trick for me several times.

from workers-types.

probablykasper avatar probablykasper commented on July 18, 2024

@penalosa I don't have libdom anywhere. My tsconfig.json is the same as what's suggested in the README, except I have "module": "esnext" instead of "CommonJS".

@eugene1g Adding /// <reference types="@cloudflare/workers-types" /> to the start of the file doesn't seem to fix it unfortunately. Also, the cf parameter isn't missing for me - I have both cf and mode, etc.

from workers-types.

penalosa avatar penalosa commented on July 18, 2024

@probablykasper If you're seeing cf and mode, that's definitely unexpected behaviour. It's most likely that libdom or libwebworker is being included somewhere (perhaps by a framework you're using?), but could you clarify the version of the types you're using, and whether @cloudflare/workers-types@4 fixes the issue?

from workers-types.

probablykasper avatar probablykasper commented on July 18, 2024

I'm using v4 now, but it was also an issue on v3

from workers-types.

penalosa avatar penalosa commented on July 18, 2024

Would you be able to set up a reproduction repository?

from workers-types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.