Effect CommunityEC
Effect Community4mo ago
7 replies
JLN

Passing a Generic Type to Another Generic Type in TypeScript

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
Was this page helpful?