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

Weird type-safety behaviour when using $ helper #132

Open
Ivanrenes opened this issue Apr 29, 2024 · 1 comment
Open

Weird type-safety behaviour when using $ helper #132

Ivanrenes opened this issue Apr 29, 2024 · 1 comment

Comments

@Ivanrenes
Copy link

Ivanrenes commented Apr 29, 2024

Hey I'm having an issue with type safety when using $ helper.

Seems like when using $ helper, we lost full type safety since TS does not complain about "inexistentProp".
image

However if we remove the $ helper, it works as expected, triggering "inexistentProp" as not part of type User
image

Versions tested:

  • 10.3.0
  • 10.7.0

If need more context lmk!

@kossnocorp
Copy link
Owner

Hey! Thank you for opening the issue. Here's what's going on:

The problem is that TypeScript has structured typing, and there's no way to fix it without making the developer experience slightly worse.

Here's a playground link with the gist of the problem.

And here's one of many TypeScripts related to it: microsoft/TypeScript#12936

TL;DR: TypeScript can't do it.

However, I found a solution that works, but as I said, it makes the experience slightly worse. I can't ship it with the v10 as it will be a breaking change, and also, I'm hesitant to introduce it as it will break so much code even though the user-code fix would be pretty trivial 😞

So here's what I did in Superstate, my other type-safe project:

form.send.submit("error", "-> errored", ($, context) =>
  $({ ...context, error: "Email is missing" })
);

Instead of returning an object, it forces to wrap it into an argument. It triggers an extra properties check that is missing from the function return but present on argument passing.

So, in Typesaurus, it would look like this:

await db.users.add(($) => $({
  name: "Sasha",
  createdAt: $.serverDate(),
  inexistentProp: ''
  //^^^^^^^^^^^^
  // Type error
}));

I think it's worth it, but as I said, it will require a breaking change, and I wanted to keep v10 stable for a long time. I could use a schema function option to switch types used for the write helpers, but I'm unsure how much work it would be and if it's worth the sacrifice to preserve v10 stability.

What do you think? I would appreciate any feedback.

@kossnocorp kossnocorp moved this to Planned in Typesaurus May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Planned
Development

No branches or pull requests

2 participants