sindresorhus / type-fest

A collection of essential TypeScript types
Creative Commons Zero v1.0 Universal
14.2k stars 537 forks source link

Add `Passthrough` type #227

Open sindresorhus opened 3 years ago

sindresorhus commented 3 years ago

https://github.com/sindresorhus/type-fest/pull/188

Upvote & Fund

Fund with Polar

voxpelli commented 3 years ago

Here's a self-contained port of the use case in #188:

namespace Foo {
  export interface InnerFoo {
    abc: {}
  }
}

type Passthrough<T> = T;

// Syntax Error
interface EnhancedAnchorProps extends Foo.InnerFoo["abc"] {

}

// Valid Syntax
interface EnhancedAnchorProps extends Passthrough<Foo.InnerFoo["abc"]> {

}

// Also valid
type abc = Foo.InnerFoo["abc"];
interface EnhancedAnchorProps extends abc {

}

While Passthrough<Foo.InnerFoo["abc"]> in a way is prettier, I'm not convinced the utility is enough to add it to type-fest?

It's a very very basic helper + one can also just as well manually extract the type prior to extending it.

You think it has merit @sindresorhus or you haven't made up your mind yet?

sindresorhus commented 3 years ago

I think it's worth adding it, if only to be able to document that it can be achieved without the utility too. Many of these types here are easy to implement, but what gives them value is the extensive docs.

fregante commented 3 years ago

Could be named to something more common like Same<T> or Identity<T>? I didn't understand what Passthrough<T> passed through.

papb commented 2 years ago

I prefer Passthrough, it's easier to search for than Same or Identity, and this particular utility isn't exactly intuitive, so I think it should be easily searchable (because the truly intuitive thing would be for TS to just work on that directly, without this workaround).