Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-nullable fields are optional in TypeScript #24

Closed
badrange opened this issue Mar 10, 2023 · 10 comments
Closed

Non-nullable fields are optional in TypeScript #24

badrange opened this issue Mar 10, 2023 · 10 comments

Comments

@badrange
Copy link

I just upgraded the extension from 0.3.2 to 0.4.2 and realized that some of the typing errors that arose in our project were caused by non-nullable fields being changed to optional.

Before the upgrade:
authors: string & ArticlesPeople[];

And after:
authors?: (any[] & ArticlesPeople[]) | null;

We're on Directus 9.23.1

@maltejur
Copy link
Owner

This behaviour was changed by @stafyniaksacha in #19. I am currently not using Directus actively, so I can't really judge whether having relational fields marked as nullable makes sense.

@badrange
Copy link
Author

This behaviour was changed by @stafyniaksacha in #19. I am currently not using Directus actively, so I can't really judge whether having relational fields marked as nullable makes sense.

Hmm, I see. Thanks for spending time on this project even though you're not using Directus at the moment!

In my opinion, if the relationship field is marked as non_nullable in the database it makes sense that the typescript type is not nullable.

@maltejur
Copy link
Owner

@stafyniaksacha what are your opinions on this?

@stafyniaksacha
Copy link
Contributor

Hum, this should be a setting as of unions.
Even if fields are marked as non nullable, they can be non requested (omited in fields during queries) in that case they are undefined, but they should not be marked as nullable.

@badrange
Copy link
Author

@stafyniaksacha we're using the Directus SDK and it somehow manages to rely information on which fields are requested or not. For example:

  const followResult = await directus.items('followings').createOne({
    user: userId,
    followed_item: companyId,
  });
  console.log(followResult?.id);
  console.log(followResult?.asdf);

Will have an error on the last line:

Property 'asdf' does not exist on type 'DefaultItem'.ts(2339)

while the typing of FollowResult is OneItem<Followings, QueryOne<Followings>, false>

Same for queries:

const pages = (
    await directus.items('pages').readByQuery({
      fields: ['id', '*', 'translations.*', 'sites.*', 'sites.pages_id'],
      (...)

Gets this typing - notice that the fields that are NOT NULL in the database are required:

const page: {
    sites: {
        pages_id: number;
        sites_id?: number | undefined;
        id: number;
    }[];
    translations: {
        body?: string | undefined;
        id: number;
        title: string;
        languages_code: string;
        pages_id: number;
    }[];
    date_created?: string | undefined;
    ... 5 more ...;
    slug: string;
}

stafyniaksacha added a commit to stafyniaksacha/directus-extension-generate-types that referenced this issue Mar 14, 2023
@stafyniaksacha
Copy link
Contributor

@stafyniaksacha we're using the Directus SDK and it somehow manages to rely information on which fields are requested or not. For example:

So, maybe the best thing to do would be to completely remove optional ? and let sdk handle this by itself, what do you think ?

@cupcakearmy
Copy link

I just wanted to add, that for creating items, this is very counter intuitive,as the required fields are missing then

maltejur added a commit that referenced this issue Apr 6, 2023
@maltejur
Copy link
Owner

maltejur commented Apr 6, 2023

Okay, I've reverted the changes. I am not sure though if that is exactly what you wan't, so you could test out the changes here before I release them as a new release in a few days:

https://github.com/maltejur/directus-extension-generate-types/releases/download/untagged-88eab6b5a852e5af0e9b/index.js

@cupcakearmy
Copy link

That's a lot! Going to test it in the next days! Sorry for the late reply

@maltejur
Copy link
Owner

maltejur commented Sep 7, 2023

@maltejur maltejur closed this as completed Sep 7, 2023
arnulfoacevedo added a commit to arnulfoacevedo/directus-gen-lang-types that referenced this issue Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants