#Pass a generic as generic

4 messages · Page 1 of 1 (latest)

dull moth
#

Is it somehow possible to pass a type that accepts a generic to a generic type? (Hard to describe what I mean exactly, check out the examples)

type DecorateBar<B extends AnyBar> = {
    log: (bar: B) => B
}

// What I am looking for
export type GenericBarDecorator<B extends Bars, Decorator> {
    // @ts-ignore
    [K in keyof B]: Decorator<B[K]>
}


const MyBars = {} as Bars

// @ts-ignore
type Usage = GenericBarDecorator<typeof MyBars, DecorateBar>

https://www.typescriptlang.org/play/?ssl=29&ssc=1&pln=1&pc=1#code/C4TwDgpgBAQghgJwDwBUB8UC8UDeUBucANgK4QDOAXFClAL4BQokUAggHYjwJayJJxOaBk3DRu5XgCUIAYwD2CACZJywBAEt2AcwA0bTt2Ejm0ACJzFcYBG5IYUCAA8b7JZI5dEGbDgZQAqCJ5bWoACgAjRGoYAEosDBgGRn9TPgQLBQRrRXtHFwg3SQkfXH9AgG0AaSgtKABrCBB5ADNYAF1qTKsbOxhq9uEUgHpRsfGJyZEGUagAdQALaygASSg4AFsg+Xl6rW0oFsUGZzBFYCg0gHFCiE1Zbm7s4FyHZ1d3dPJ9J5yEDD8gSgswAAsByABaDTadiKCDlALVWrsBpNVodLqWZ6vAZDaYKdhqKAAWS8CEkvjo62KiHIIlB4KhMLhohYAFVyHBtNBsDd2HcNA9EL8XshTOjSRJ9I8sX9hEA

grand flume
#

I believe you're looking for HKTs (higher kinded types) which unfortunately aren't supported natively within typescript. It's by far my biggest feature request too. There's an awesome library by alice poteat that performs some black magic to achieve that though: https://github.com/poteat/hkt-toolbelt

P.S. I'm very new to that concept so I unfortunately can't tell much about it.

GitHub

✨Functional and composable type utilities. Contribute to poteat/hkt-toolbelt development by creating an account on GitHub.

dull moth
# grand flume I believe you're looking for HKTs (higher kinded types) which unfortunately aren...

ye i think so aswell, managed to get a somewhat working version with https://github.com/geoffreytools/free-types/tree/public

GitHub

A type-level library enabling the creation and the manipulation of type constructors which can be detached from their type parameters. Also referred to as higher kinded types. - geoffreytools/free-...

#

will check out hkt-toolbelt tho