As a follow on to yesterday’s post (inspired by @dagda1's question), what is the difference between using any[], unknown[] or never[] as constraints to the function rest parameter in #TypeScript ?

1/6🧵

The answer is that if you use a type parameter for the args, there isn’t much difference. You can’t call the function with anything that isn’t typed as A, since you can’t assign a concrete type to a type parameter.
2/6🧵
typescriptlang.org/play?#code/GYV…
If the rest parameter is in a function type constraint (the way I recommend you don’t do in the previous post), then we get differences.
3/6🧵
F extends (...a: unknown[]) => void is a function that should be able to handle any arguments, so we can call it with anything. This means if we try to pass in, for example, a function taking a string we get an error.
F extends (...a: never[]) => void is a function we can’t call, since nothing will ever be compatible with never. But since we can’t call it, it is safe to pass in any function
F extends (...a: any[]) => void is the more unsafe version (any is unsafe, what a surprise!). We can both pass in any function AND call the function with any arguments, which is probably not a good idea.

typescriptlang.org/play?#code/FAM…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Titian Cernicova Dragomir 💙💛

Titian Cernicova Dragomir 💙💛 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @TitianCernicova

Mar 8
A classic problem in Typescript is warping a function in another function. Maybe also changing the parameter in some way in the process. Such as in this questions stackoverflow.com/a/71351477/125… or this one stackoverflow.com/a/71371775/125…
1/5🧵
The first problem is usually how we choose our type parameters. There are two common approaches.
2/5🧵
Define a type parameter for the whole function, then use Parameters and ReturnType. While this approach might work in most cases, usually the types of interest are the parameters and return type, not the whole function type.
3/5🧵
Read 5 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(