question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ModelObject type maps optional properties as required

See original GitHub issue

Greetings, I’m using objection@2.2.3 and typescript@4.0.2. Let’s say that I have a simple class like this:

class PersonModel extends Model {
  id: string;
  name: string;
  phone?: number;
}

When I use ModelObject<PersonModel> to create a POJO typing of this class, it gives me this result:

type PersonPojo = {
    id: string;
    name: string;
    phone: number | undefined;
}

As you can see, phone is an optional property in PersonModel class. ModelObject maps it to undefined too, but makes it an required property. I’m using graphql-codegen and to map the typings of the generated response types to my classes, I use ModelObject. Because of it maps these kind of properties as required, if you have lots of relationships under PersonModel, it wants you to set all of those properties (as undefined or its own value type).

To not miss which properties are optional, I’ve modified ModelObject like this:

type ModelObject2<T extends Model> = Pick<
  T,
  Exclude<NonFunctionPropertyNames<T>, 'QueryBuilderType'>
>;

And when I use ModelObject2<PersonModel>, it protects the optional properties like this:

type PersonPojo = {
    id: string;
    name: string;
    phone?: number | undefined;
}

I’m not sure if this is an expected behavior of ModelObject but wanted to give a heads up about this. Mine can be an edge case which may require a custom solution like ModelObject2 of course. But other people may bump into this kind of an issue too.

Thanks for this great package!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

4reactions
knicolacommented, Feb 13, 2022

Tested w/ Typescript 4.5.5

import { NonFunctionPropertyNames } from 'objection'
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>
type ModelObject<T> = Omit<NonFunctionProperties<T>, 'QueryBuilderType'>
type PersonModelShape = ModelObject<PersonModel>
1reaction
chrishuman0923commented, Nov 9, 2021

Same problem here. Thanks for the workaround @onderonur !

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: Transforming optional properties to required ...
This works by mapping over Required<T> , a version of our original type where all the optional properties have been replaced by required...
Read more >
Sling Models - Apache Sling
Names; Optional and Required; Defaults; Collections ... the model object; an exporter name; a target class; a map of options.
Read more >
Models - Django documentation
Generally, each model maps to a single database table. The basics: Each model is a Python class that subclasses django.db.models.Model .
Read more >
<Mapping> elements - IBM
The elements define the mapping between a business model object type in ... Note: It is not necessary for the Rational Asset Manager...
Read more >
typescript - Keep `?:` types optional when looping over them
If you're looking to take a type T and a union of its keys K and produce a new type where the properties...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found