Giter Site home page Giter Site logo

IncomingRequestCfPropertiesGeographicInformation makes geo property access an error (regression from 3.17.0) about workers-types HOT 7 CLOSED

maxshirshin avatar maxshirshin commented on July 18, 2024 8
IncomingRequestCfPropertiesGeographicInformation makes geo property access an error (regression from 3.17.0)

from workers-types.

Comments (7)

Qantas94Heavy avatar Qantas94Heavy commented on July 18, 2024 4

While the type is technically valid, the change did break existing code in a minor version upgrade (without a breaking change in the actual Workers runtime). As such I'd support allowing optional chaining again and leaving any discussion of changing this to V4.

from workers-types.

philipatkinson avatar philipatkinson commented on July 18, 2024 3

I guess this is question of whether it is reasonable to support optional chaining on the (possible) cf property or not? My pull request was ultimately because I do use optional chaining (rather than the property check method noted above). Seems that both are technically valid?

from workers-types.

caass avatar caass commented on July 18, 2024 1

I think the new types are technically correct -- the way that the geographic information is gathered in Cloudflare's edge, there really may not be a city property. Is there any downside to @Qantas94Heavy's suggestion of using city in req.cf for type narrowing? You could potentially write a function like

function hasGeographicInformation(cf: IncomingRequestCfPropertiesGeographicInformation): city is Extract<IncomingRequestCfProperties, { country: Iso3166Alpha2Code }> {
  return "country" in cf && cf.country !== "T1"
}

// works!
if (hasGeographicInformation(cf)) {
  console.log(cf.city)
}

Playground link

from workers-types.

philipatkinson avatar philipatkinson commented on July 18, 2024

I have a pull request that should fix this: #310.

from workers-types.

Qantas94Heavy avatar Qantas94Heavy commented on July 18, 2024

Technically you can get the original to work if you perform an in check to narrow the type down, e.g.

if (req.cf && 'city' in req.cf) {
  console.log(req.cf.city);
  console.log(req.cf.continent);
}

However this can be sometimes unwieldy, especially given ?. is not used for type narrowing.

from workers-types.

caass avatar caass commented on July 18, 2024

Yeah, I see the issue. I wonder if there's a way to have the type be both semantically correct and also allow optional chaining...

from workers-types.

lostpebble avatar lostpebble commented on July 18, 2024

This is quite a big regression and the solutions are very unwieldy.

This is one of those niggles of TypeScript for me- when you are creating a large union type with | (or) - you need to actually include all the properties in each object type (just mark them as { city?: undefined } if they are undefined in a specific form). Or have an enum propery we can check against to discriminate between them.

If TypeScript can't know for sure that a property within an object exists (even if it exists in one of the subsets of the union types), it will throw this error we see here.

Yeah, I see the issue. I wonder if there's a way to have the type be both semantically correct and also allow optional chaining...

I think doing what I suggested above (actually naming the properties but making them ?: undefined) should work...

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.