Can someone explain why types aren't inferred
If I use this code then the types arent inferred whereas with the code in the second image I can
9 Replies
What’s the difference between the code? It looks the same
The difference is that on the left, he doesn't chain methods
In my case too, without chaining, type inference doesn't work
I did some research on this here is what I found
"TypeScript infers and preserves type information through method chaining directly at assignment versus modifying an instance with method calls in subsequent lines."
So pretty much when dealing with a class TS takes the types of the class when it is initiated. When you chain onto it's appened to the class. When you call it later it does not modify the types that were inferred. I belive this is why when using RPC all the routes have to be chained
Yes, and it's a problem in the sense that the documentation is full of examples that don't use chaining. I personally chose to chain all the methods on my project, but I have to admit that this has an impact on readability. Here's a related issue: https://github.com/honojs/hono/issues/2120#issuecomment-1917847801
GitHub
RPC Client isn't compatible with app.route() · Issue #2120 · honojs...
What version of Hono are you using? 3.12.8 What runtime/platform is your app running on? Bun What steps can reproduce the bug? // routes/routerA.ts import { Hono } from "hono"; import { z...
I think that is why Elysia requires chaining or at least only shows docs with chaning last time I looked
Yes if it's a Typescript limitation then you have to live with it ^^
I know typescript has a big update coming, I don't know if this is something that they will solve
Do you think this kind of functionality would be added via Typescript 5.5 - Inferred Type Predicates ?
Daniel Rosenwasser
TypeScript
Announcing TypeScript 5.5 RC - TypeScript
Today we are excited to announce the availability of the release candidate of TypeScript 5.5. To get started using the RC, you can get it through NuGet, or through npm with the following command: npm install -D typescript@rc Here’s a quick list of what’s new in TypeScript 5.5!
I was just looking through it, I doesn't appear so
But there are a few other type inference issues they are fixing