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

Enables strict null checks in SDK #2360

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Conversation

infomiho
Copy link
Contributor

@infomiho infomiho commented Oct 28, 2024

This PR enables strictNullChecks option in the SDK tsconfig.json.

We want to enable this option to make sure Zod schemas are working properly before we start using them for env variables validation.

Left to do

  • Rough changes to get it to work
  • Go through changes one more time
    • Keep changes that are straightforward and long term
    • Add @ts-ignore and a TODO for things that need more work

Using @ts-ignore is not that problematic since it will unblock us for using Zod schemas, but also explicitly mark parts of the code base that need some work. This work was still needed before this PR, but it was implicit.

@@ -302,7 +302,7 @@ function AdditionalFormFields({
disabled={isLoading}
/>
{errors[field.name] && (
<FormError>{errors[field.name].message}</FormError>
<FormError>{errors[field.name]!.message}</FormError>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this is needed since we have the check errors[field.name] && (?

@@ -69,7 +69,7 @@ async function getAuthUserData(userId: {= userEntityUpper =}['id']): Promise<Aut
throwInvalidCredentialsError()
}

return createAuthUserData(user);
return createAuthUserData(user!);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I'm not sure why this is needed since we have the check if (!user) {. Maybe it's not obvious to the compiler that the throwInvalidCredentialsError fn throws?

const { data: task, isLoading } = tasksCrud.get.useQuery({
id: parseInt(id, 10),
});
const { id } = useParams<{ id: string }>()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, but a quick fix since id is of type string | undefined.

@@ -18,7 +18,7 @@ import {
// Details here: https://github.com/wasp-lang/wasp/issues/2017
export function makeQueryCacheKey<Input, Output>(
query: Query<Input, Output>,
payload: Input
payload?: Input
Copy link
Contributor Author

@infomiho infomiho Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

payload !== undefined hints at payload being optional

@@ -1,9 +1,9 @@
{{={= =}=}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've started to work on enabling strict null checks while working on env variables, so these are some changes I left from that effort - env -> nodeEnv since I imagined that the import of env vars would look like import { env } from 'wasp/server'

@@ -93,4 +93,5 @@ type ClientOperationWithNonAnyInput<Input, Output> =
? (args?: unknown) => Promise<Output>
: [Input] extends [void]
? () => Promise<Output>
: (args: Input) => Promise<Output>
// TODO: decide if this is what we want?
: (args?: Input) => Promise<Output>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We get the error:

client/operations/hooks.ts(32,26): error TS2345: Argument of type 'Input | undefined' is not assignable to parameter of type 'Input'.

The useQuery hook definition:

function useQuery<Input, Output>(
  query: Query<Input, Output>,
  queryFnArgs?: Input,
  options?: any
): UseQueryResult<Output, Error>

has the queryFnArgs as an optional argument: and it executes query(queryFnArgs), query is:

query: Query<Input, Output>

which is in the end (args: Input) => Promise<Output>.

This leads me to set the args as optional here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sodic alternativelly, I can just put a @ts-ignore and not modify anything related to operations, so we can deal with this in the future?

@infomiho infomiho changed the title WIP: Enables strict null checks in SDK Enables strict null checks in SDK Oct 28, 2024
@infomiho infomiho requested a review from sodic October 28, 2024 15:04
@infomiho infomiho mentioned this pull request Oct 29, 2024
5 tasks
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

Successfully merging this pull request may close these issues.

1 participant