Comments (7)
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.
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.
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)
}
from workers-types.
I have a pull request that should fix this: #310.
from workers-types.
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.
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.
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)
- Missing properties on IncomingRequestCfPropertiesBotManagement HOT 1
- Incorrect D1 types HOT 2
- RequestInit is missing some properties like 'cache' HOT 3
- Generate types from `workerd` RTTI HOT 1
- RFC: Exportable types HOT 7
- Fix `request.cf` types
- Generate exportable version of `workers-types` HOT 1
- add JSDocs documentation in types
- Move overrides to `workerd` C++ source code HOT 1
- Include C++ parameter names in types generated from JSG RTTI HOT 2
- Get type hints on incoming Request object HOT 4
- HtmlRewriter ElementHandler methods can only accept strings (not general Content) HOT 1
- (3.18.0) IncomingRequestCfPropertiesGeographicInformation is the wrong type HOT 2
- Class 'Request<ParamKeyType, Data>' incorrectly extends base class 'Body' HOT 1
- `new Request` bad type HOT 8
- addEventListener has a broken type definition HOT 1
- NPM Release @cloudflare/worker-types 4.20221111.0 HOT 2
- Add Symbol.iterator to WebScoketPair HOT 1
- What is Fetcher for? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from workers-types.