I've just had to write some overloads for this exactly problem last week. That's awesome to know that this'll no longer be a problem in version 5.8!
@Typed-Rocks13 күн бұрын
Awesome. I‘m also looking forward to it
@dmitryts909312 күн бұрын
Could it be easier to create two types?
@ThiagoOliveiraSantosfaren12 күн бұрын
@ there're plenty of scenarios where you may create a single function that returns many different types according to its parameter with no additional logic. If you have opportunity to make code simpler, typescript shines giving precise return types
@DirkLuijk12 күн бұрын
Finally! Been waiting for this feature for a long time. Had to depend either on overloading (which doesn’t actually check whether the implementation is consistent with the overloading contract!), or a dirty type-cast.
@malvoliosf13 күн бұрын
This doesn’t sound like a feature so much as a bug fix. I would have thought that it would work in 5.7.
@Typed-Rocks13 күн бұрын
You have to check the pullrequest. There it is explained why it wasn‘t possible before. It „kinda“ is a bug but it is explainable why it was the case.
@dzienisz12 күн бұрын
TS is mostly fixing on bugs. I think feature set was done years ago.
@keteremillpario10 күн бұрын
Ts is a joke...
@tamirbahar13 күн бұрын
Thanks for the video! Now I'm eagerly awaiting the 5.8 release
@Typed-Rocks13 күн бұрын
Me too. Glad you like it :)
@TheCoonHouse12 күн бұрын
Not sure if I understand this. Why wouldn't you create separate types for "PersonType" and "CompanyType"? What's the benefit of doing it this way?
@Betadesk12 күн бұрын
Just to flex some TypeScript magic
@Typed-Rocks11 күн бұрын
I wanted to use a similar example as in the pull request where it was merged to make it easier for the reader of the pr to understand.
@TheCoonHouse10 күн бұрын
@@Typed-Rocks Could you give an example of where this would be useful? Always willing to learn.
@rajikkali10 күн бұрын
Yeah, looks like bad coding practice to me too
@benny123tw5 күн бұрын
Well explain!
@LostZenith_12 күн бұрын
I don't know about write less code, but it's definately a lot more readable. Excellent change indeed!
@Typed-Rocks12 күн бұрын
Absolutely. The thing aber better readability is always a plus
works even without that mapped type annotation in const mockData
@mikuhl12 күн бұрын
This is not a great solution for performance sensitive applications.
@Krmpfpks10 күн бұрын
@@mikuhli doubt that it is slower in any JS runtime, they all will optimize that out.
@eliuddyn12 күн бұрын
Amazing 🔥🔥
@Typed-Rocks12 күн бұрын
Thanks 🔥
@thechoephix12 күн бұрын
Finally!
@zeratax6 күн бұрын
this was one of my biggest issues with typescript tbh
@jon186712 күн бұрын
This is super nice!
@Typed-Rocks12 күн бұрын
Glad you like it!
@pookiepats11 күн бұрын
When your Linter gets a cool new feature
@coder_one5 күн бұрын
In all these years of commercial work, I have not missed this feature. What are its actual real world use cases (not so trivial like mocking data)?
@Typed-Rocks5 күн бұрын
Often it's for library authors. In a normal codebase you rarely need these advanced types. That's 100% true.
@thejimmylin6 күн бұрын
What is the nice // ^? feature from?
@Typed-Rocks6 күн бұрын
Its from a plugin called „twoslash-queries“
@thejimmylin6 күн бұрын
@@Typed-RocksThank you so much!😊
@meuscc13 күн бұрын
need partial type inference and pipe operator
@Typed-Rocks13 күн бұрын
Maybe in the future
@moh682312 күн бұрын
You can just use keyof instead of overloading the function. It would result in the same code?
@Typed-Rocks12 күн бұрын
In the final one, this is true 👍. But we don‘t need that anymore with 5.8
@darkerisbetter869913 күн бұрын
I’ve wanted this feature to narrow arrays. The overload style is gross (though I have used it).
@Typed-Rocks13 күн бұрын
I agree. The new approach is really nice and concise 🤘
@ErnaSolbergXXX10 күн бұрын
This just seems like one more way to create a messy code base with typescript.
@giovanesouza86648 күн бұрын
JS devs got types and now they do this with them... Why?
@taquanminhlong12 күн бұрын
Good to see typescript being less dump 😂
@SaurabhSrivastava-i1q10 күн бұрын
I am not writing types which has become another programming language itself!
@tesilab99410 күн бұрын
How very strange to illustrate this example with a "UserType" which isn't really a type, but just a holder for unrelated types each with its own property name. The example would be better if your property names were capitalized, and each one independently defined as a type, and then you combined them into so a union.
@zohnannor12 күн бұрын
bro, please, increase the bitrate when exporting/recording the video. your videos are great, but all those artifacts ruin the experience!
@Typed-Rocks12 күн бұрын
Interesting. Are you watching it on a pc? I‘m exporting it with h.264 from final cut as mp4. Sometimes KZbin does scale it down :(
@zohnannor12 күн бұрын
@@Typed-Rocks yeah, I'm on pc, watching in 1080p. I constantly see those "flashing rectangles" of artifacts, and the edge of the circle of your webcam is very blurry hard to see :( I don't really know how to describe the artifacts lol
@pantherofsteel9 күн бұрын
Being so much excited for TS is one of the saddest things that I saw on internet today.
@Typed-Rocks9 күн бұрын
Then you must have an awesome curated internet feed 🤘
@cenkakay350611 күн бұрын
yeah switch case really messy
@dzienisz12 күн бұрын
TypeScript is the most complicated thing in JS world. I just personally hate it.
@Typed-Rocks12 күн бұрын
Sad to hear. But I can understand
@Betadesk12 күн бұрын
Yeah I believe the type system is Turing complete lmao, but just cause it can be complicated doesnt mean it has to be. For example you could easily just make a mockPerson() and mockCompany() function whose types are straightforward
@coder_one11 күн бұрын
@dzienisz perhaps jsDoc?
@hugodsa8912 күн бұрын
Dude instead of overloading you could do an XOR
@LordErnie12 күн бұрын
Although I love TypeScript, these are the moments where I think its just still lacking. Really with all of the different ways we can use typing in TypeScript I'd imagine this would be a base feature in the first place.
@Typed-Rocks12 күн бұрын
I was also expecting that but the language is always evolving.
@ShinSpiegel12 күн бұрын
Unpopular opinion, the new code look uglier.
@Typed-Rocks12 күн бұрын
Maybe for this simple case. But if you have more cases, the overloading would be quite repetitive. But everyone to their likings
@ShinSpiegel12 күн бұрын
@ repetitive, yes. But also expressive. It shows what expect, generics on this level makes the code “unpredictable” since it depends on other types (that can or cannot exist closer) to the function. Either way, it was already a unpopular opinion.
@userasd36012 күн бұрын
type UserType = { person: { firstName: string; lastName: string }; company: { name: string }; }; function mockUser(type: T): UserType[T] { switch (type) { case "person": return { firstName: "John", lastName: "Doe" } as UserType[T]; case "company": return { name: "ACME" } as UserType[T]; default: return undefined as unknown as UserType[T]; } } Just for the return type to match each return is forcefully type casted to that return type or need to create overloaded functions for the same as suggested by you.
@Typed-Rocks12 күн бұрын
Also a great way👍
@airman12246911 күн бұрын
Until TypeScript has strict TYPES, I really couldn’t give less of a shit about its other features.