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

Fix typechecking (#1798) #2250

Conversation

trumbitta
Copy link
Contributor

Fixes a resurgence of the bug found in #1798

It might be a breaking change because it forces code like this:

  const draw = useControl<MapboxGlDraw>(
    () => new MapboxGlDraw(rest),
    ({ map }: MapContextValue<MapInstance>) => {
      map.on("draw.create", onCreate);
    },
    ({ map }: MapContextValue<MapInstance>) => {
      map.off("draw.create", onCreate);
    },
    {
      position,
    },
  );

to become:

  const draw = useControl<MapboxGlDraw>(
    () => new MapboxGlDraw(rest),
    ({ map }: MapContextValue<MapInstance>) => {
      map?.on("draw.create", onCreate);
    },
    ({ map }: MapContextValue<MapInstance>) => {
      map?.off("draw.create", onCreate);
    },
    {
      position,
    },
  );

@Pessimistress
Copy link
Collaborator

That does not look like the right solution. If you need to make something optional, you add | undefined to where you use it, not in its type definition.

Can you create a project that reproduces your issue?

@trumbitta
Copy link
Contributor Author

That does not look like the right solution. If you need to make something optional, you add | undefined to where you use it, not in its type definition.

Can you create a project that reproduces your issue?

It's the same issue as #1798, although happening with plain tsc instead of Vite, and it's also the same fix as #1799

I'll try to put together a repro 🙏

@trumbitta
Copy link
Contributor Author

@Pessimistress here's the repro!

https://codesandbox.io/p/sandbox/wispy-thunder-tx58mq

Run yarn tsc or yarn start if it doesn't start automatically.

@Pessimistress
Copy link
Collaborator

it's also the same fix as #1799

No, it's not the same fix. You need to change it where MapRef is used, not where it is defined.

Looks like TypeScript compiler is not inferring the return type of useMap correctly. You can enforce it like this:

--- a/src/components/use-map.tsx
+++ b/src/components/use-map.tsx
@@ -52,7 +52,10 @@ export const MapProvider: React.FC<{children?: React.ReactNode}> = props => {
   );
 };

-export function useMap<MapT extends MapInstance>() {
+export function useMap<MapT extends MapInstance>(): {
+  [id: string]: MapRef<MapT> | undefined;
+  current?: MapRef<MapT>;
+} {
   const maps = useContext(MountedMapsContext)?.maps;
   const currentMap = useContext(MapContext);

@trumbitta
Copy link
Contributor Author

trumbitta commented Aug 10, 2023

@Pessimistress It kind of makes sense, but I don't think I know how to test this kind of change in this repo.
My change I could test locally by editing the definition files in node_modules, but this seems to require first compiling, then other steps I'm not familiar with.

Would you mind fixing this yourself the way you suggested? 🙏

EDIT: I implemented the fix, in case you are 100% sure and would like to just merge it

@trumbitta trumbitta force-pushed the trumbitta/bug-incorrect-types-causing-1798 branch 2 times, most recently from eeba807 to 297c83a Compare August 10, 2023 23:05
@trumbitta trumbitta force-pushed the trumbitta/bug-incorrect-types-causing-1798 branch from 297c83a to 2aa69e0 Compare August 11, 2023 12:33
@Pessimistress Pessimistress merged commit 6e166cf into visgl:master Aug 11, 2023
2 checks passed
@jscheid
Copy link

jscheid commented Aug 14, 2023

@Pessimistress thanks for the merge! Any chance for a new release with this fix?

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.

None yet

3 participants