|
@MatthewGerstman | |||||
|
Give me your best real world use cases of conditional types and inference types in TypeScript. Can be open source or not.
Retweets appreciated.
|
||||||
|
||||||
|
Josh Goldberg
@JoshuaKGoldberg
|
27. sij |
|
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/?
|
||
|
|
||
|
Josh Goldberg
@JoshuaKGoldberg
|
27. sij |
|
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
|
||
|
|
||
|
Simon Korzunov
@twopSK
|
27. sij |
|
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. pic.twitter.com/dMcVyhy7Bl
|
||
|
|
||
|
Josh Goldberg
@JoshuaKGoldberg
|
27. sij |
|
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: github.com/Codecademy/con…
|
||
|
|
||
|
Joachim Viide
@jviide
|
27. sij |
|
Matchkin (github.com/jviide/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 🙂 pic.twitter.com/OORQoiSxA0
|
||
|
|
||
|
Ethan Arrowood 👻
@ArrowoodTech
|
28. sij |
|
Oh do I have some content for you - will message when I get home
|
||
|
|
||
|
Eric Zorn
@zornwebdev
|
27. sij |
|
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
|
||
|
|
||
|
Guy Balaam
@iambalaam
|
27. sij |
|
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
|
||
|
|
||