Twitter | Pretraživanje | |
Matthew
Give me your best real world use cases of conditional types and inference types in TypeScript. Can be open source or not. Retweets appreciated.
Reply Retweet Označi sa "sviđa mi se" More
Josh Goldberg 27. sij
Odgovor korisniku/ci @MatthewGerstman
I'm working on a function that returns { setTimeout } when called in an environment where Jest does not exist, and { setTimeout: jest.fn() } when it does. declare namespace jest {} export type JestExists = typeof jest extends { fn: Function } ? true : false; 1/?
Reply Retweet Označi sa "sviđa mi se"
Josh Goldberg 27. sij
Odgovor korisniku/ci @MatthewGerstman
The function's return type is something like: JestExists ? Pick<Window, 'setTimeout'> : { setTimeout: jest.Mock }. Relies on *.test.* files being included in a separate tsconfig project than your source files Pretty cool IMO 😎 2/2
Reply Retweet Označi sa "sviđa mi se"
Simon Korzunov 27. sij
Odgovor korisniku/ci @MatthewGerstman
working on a library to add side effects to "useReducer". That allows to write this code. All of it is typechecked thanks to conditional types.
Reply Retweet Označi sa "sviđa mi se"
Josh Goldberg 27. sij
Odgovor korisniku/ci @MatthewGerstman
Oh also, for a library that needs to replace everything on the Console object, this type removes the non-Function parts to leave a type with just the member functions:
Reply Retweet Označi sa "sviđa mi se"
Joachim Viide 27. sij
Odgovor korisniku/ci @MatthewGerstman @jonihasanen
Matchkin () contorts conditional & inference types to implement statically checked exhaustive matching. Maybe it would've been possible to get something similar w/o those specific TS features. It has its uses, though. Sorry for the potato quality video 🙂
Reply Retweet Označi sa "sviđa mi se"
Ethan Arrowood 👻 28. sij
Odgovor korisniku/ci @MatthewGerstman
Oh do I have some content for you - will message when I get home
Reply Retweet Označi sa "sviđa mi se"
Eric Zorn 27. sij
Odgovor korisniku/ci @MatthewGerstman
Well the best case I’ve seen is when an API I was using sent back an integer value as both a number and a string. It completely occurred randomly
Reply Retweet Označi sa "sviđa mi se"
Guy Balaam 27. sij
Odgovor korisniku/ci @MatthewGerstman
We had an api client that had methods for our backend services. We wanted to be able to load test new features on our production hosts without leaking the names of endpoints. Using conditional types helped us keep type safety without exposing a runtime artifact
Reply Retweet Označi sa "sviđa mi se"