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

Type error when adding an already expanded ReverseNavigationResource in the $select list #1342

Open
barribarrier opened this issue May 29, 2023 · 2 comments

Comments

@barribarrier
Copy link

Expected Behavior

No type error

Actual Behavior

With strictNullChecks set to true, selecting 'device_tag' field results in type error.
This is not specific to only 'device_tag' field. Any field that has a type ReverseNavigationResource<T> fails.

Type '"device_tag"' is not assignable to type '"id" | "actor" | "created_at" | "modified_at" | "custom_latitude" | "custom_longitude" | "device_name" | "download_progress" | "ip_address" | "public_address" | "mac_address" | ... 43 more ... | "should_be_managed_by__supervisor_release"'. ts(2322)

With strictNullChecks set to false, there's no error.

Steps to Reproduce the Problem

git clone https://github.com/barribarrier/strictNullChecks-balena-sdk-issue.git
cd strictNullChecks-balena-sdk-issue
npm install
npm run build

OR

  1. tsconfig.json
{
  "compilerOptions": {
    "strictNullChecks": true
  }
}
  1. index.ts
import { getSdk } from 'balena-sdk';

const balena = getSdk();

balena.models.device.getAllByApplication(
  '123456',
  {
    $select: ['uuid', 'device_tag', 'device_environment_variable', 'created_at'],
    $expand: {
      device_tag: {
        $select: ['tag_key', 'value'],
      },
    },
  },
);
  1. Run
tsc

Specifications

  • SDK version: 17.1.2
  • Browser:
  • Node version: v19.3.0
  • Npm version: 9.2.0
  • Typescript version: 5.0.4
  • OS: Ubuntu

References

@thgreasi
Copy link
Member

thgreasi commented Jun 1, 2023

Hi,
Thanks for raising this and for the POC repo.
I think this is actually the intended behavior, since ReverseNavigationResource properties in $select have no effect and they normally are supposed to be provided in $expands.
The query that you provided above will actually throw a 500 error when running against balenaCloud, because of having the device_environment_variable ReverseNavigationResource in the $select.

The part that this becomes a bit weird though is that having the device_tag in the $select happens to be fine by the backend as long as it is also part of an $expand.
The SDK's typings are trying to be slightly stricter on this and block ReverseNavigationResource properties in $selects completely (rather than checking whether they also show up in the $expand) to protect the user in the common case.
The typings are explicitly handling this case here:

: Exclude<StringKeyof<T>, PropsOfType<T, ReverseNavigationResource<{}>>>; // This is the normal typed case

Let us know your thoughts on this.

@barribarrier
Copy link
Author

Oh, I didn't know that. I just assumed specifying them in $select was mandatory.
Thanks.

@thgreasi thgreasi changed the title Type error when "strictNullChecks: true" Type error when adding an already expanded ReverseNavigationResource in the $select list Jun 1, 2023
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

2 participants