Skip to content

How to write a generic method in typescript? #6863

Answered by electrovir
willin asked this question in Q&A
Discussion options

You must be logged in to vote

You've gotta have at least one as cast somewhere but here you go:

edit: simplified

import {Prisma, PrismaClient} from '@prisma/client';

export const client = new PrismaClient();

type GenericModel = Prisma.PostDelegate<any>;

function getModel<SpecificModelName extends ModelName>(
    inputClient: PrismaClient,
    modelName: SpecificModelName,
): GenericModel {
    return inputClient[modelName] as GenericModel;
}

// https://github.com/prisma/prisma/issues/11940#issuecomment-1049572440
type IgnorePrismaBuiltins<PrismaClientKeyUnionGeneric extends string> =
    string extends PrismaClientKeyUnionGeneric
        ? string
        : PrismaClientKeyUnionGeneric extends ''
        ? PrismaCli…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@willin
Comment options

@electrovir
Comment options

@ptrxyz
Comment options

Answer selected by willin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants