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

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật