Is there a way to programmatically declare a type based on parameters provided to its “template” in TypeScript?

  Kiến thức lập trình

I would like to setup a template for my database models which can be returned in few different ways:

  • as queried object (result from a database query, e.g. SELECT statement)
  • as insertable object (body of the INSERT statement)
  • as queried object with relations (like queried, but also specifying additional types which may be present following a JOIN statement)
  • as insertable object with relations (like insertable, but also specifying additional types which may be used in a graph-insert (handled by the ORM))

I wanted to specify a template that outlines three different types of properties on the objects, which I could then re-use across different ORM models, so that I don’t have to redeclare the relationships between them. This should make more sense with the following example. The reason why this is more complicated than simply specifying three types for each of the models, is the fact that the database itself automatically sets additional properties during the insertion, which may (but don’t need to be) present in the INSERT statement itself. Here is a basic demo:

type AutoSetProperties = {
    id: string              // This property is set automatically by database, unless provided
}

type Properties = {
    value: number           // This property is required
    title?: string | null   // This property is optional and will be set to null if not present
}

type Relations<Insertable = false> = {
    someRelation?: (Insertable extends true ? SomeModel<true, true> : SomeModel<false, true>)
}

type ModelType<IsInsertable = false, HasRelations = false> = {} 
        & (IsInsertable extends true ? Partial<AutoSetProperties> : AutoSetProperties)
        & Properties
        & (HasRelations extends true ? (
            (IsInsertable extends true ? Relations<true> : Relations)
        ) : {})


// This is just for parity of the example, normally this would be another model with its own set of autoSetProperties, properties and relations
// (imported from another file defining SomeModel)
type SomeModel<I,Q> = ModelType<I, Q>


// My goal would then be to be able to do the following:
const MyModel<false, true> = getModels({relations: {someRelation: true}}) 

I’m aware that this is already fairly messy and I’ve resorted to this approach after countless attempts of specifying a model which can declare some properties as optional conditionally (e.g. to set the autoSetProperties). Optimally, what I would really want is the following:


type MyModel<IsInsertable, HasRelationships> {
   id: (IsInsertable extends true ? Optional<string> : string) // but Optional<> does not exist and making it `string | undefined` still requires me to specify id as undefined when inserting, which messes with the ORM.
}

Question(s)

  1. How do I define the above template that I can then instantiate with my own set of properties?
  2. Can I somehow make it into a generic type that accepts AutoSetProperties, Properties and Relations and returns a type with two other generics (e.g. IsInsertable and HasRelationships), like so:
// Types ASP, P, R are normal types with properties defined, as above.

type MyModel = ModelType<ASP, P, R>
const a = MyModel<true, false> // etc.
  1. Are there any better approaches to this problem that come to mind?

LEAVE A COMMENT