Skip to content

ROU-12774: Fix TerraDraw dispose crash and TypeScript type errors #260

Merged
rugoncalves merged 12 commits intodevfrom
ROU-12774
Apr 27, 2026
Merged

ROU-12774: Fix TerraDraw dispose crash and TypeScript type errors #260
rugoncalves merged 12 commits intodevfrom
ROU-12774

Conversation

@rugoncalves
Copy link
Copy Markdown
Contributor

This PR fixes a crash when disposing TerraDraw drawing tools that were never activated, and resolves several TypeScript compiler errors in the TerraDraw and deck.gl providers.

What was happening

  • Disposing a map with drawing tools configured but never activated caused a crash. dispose() was calling setMode() and stop() unconditionally, but TerraDraw throws when stop() is called on an instance that was never started (i.e., enabled is false).
  • Using globalThis.terraDraw, globalThis.terraDrawGoogleMapsAdapter, and globalThis.deck caused TS error 7017. Runtime-injected browser globals are declared via interface Window augmentation in Global.d.ts, which TypeScript only resolves through window, not globalThis.
  • DrawConfig.getProviderConfig() was typed as unknown[] (array), but all implementations return a plain options object { styles: { ... } }. Passing unknown[] to TerraDraw mode constructors triggered TS2559 ("Type 'unknown[]' has no properties in common with...") across all five Draw tool classes.
  • Polygon._createProvider passed path as Array<Coordinates> where google.maps.LatLngLiteral[] was expected, causing a type mismatch.

What was done

  • Guarded the setMode() / stop() calls in dispose() behind a this.provider.enabled check. If the drawing tool was built but the user never interacted with it, the TerraDraw instance is never started and must be skipped during teardown.
  • Replaced globalThis with window across all TerraDraw (DrawingTools.ts, all DrawXxx.ts tools) and deck.gl (HeatmapLayer.ts) provider files. Added a project-level SonarCloud suppression for rule typescript:S7764 in sonar-project.properties — the window pattern is correct here given the Window augmentation strategy.
  • Changed DrawConfig.getProviderConfig() return type from unknown[] to unknown. Removed the as unknown as unknown[] double-cast from all three config subclasses. Updated all five createTerraDrawMode() callers to cast via ConstructorParameters<typeof window.terraDraw.TerraDrawXxxMode>[0], which derives the expected options type directly from the constructor signature.
  • Added as google.maps.LatLngLiteral[] cast to path in Polygon._createProvider.

Test Steps

Hide Drawing Tools error

  1. Enter test page
  2. Hide the Drawing Tools
  3. Validate that no error appears

Type errors

  1. Run npm run build command
  2. Validate that the build is successful

Screenshots

(prefer animated gif)

Checklist

  • tested locally
  • documented the code
  • clean all warnings and errors of eslint
  • requires changes in OutSystems (if so, provide a module with changes)
  • requires new sample page in OutSystems (if so, provide a module with changes)

This rule is not relevant for our project, as this rule is intended to improve portability, consistency, and testing across different JavaScript environments (browsers, Web Workers, and Node.js).
Copilot AI review requested due to automatic review settings April 27, 2026 10:57
@rugoncalves rugoncalves requested a review from a team as a code owner April 27, 2026 10:57
@rugoncalves rugoncalves added the bug Something isn't working label Apr 27, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses stability and TypeScript typing issues in the TerraDraw drawing tools integration and related provider code (deck.gl + Google Maps shapes), primarily by guarding teardown logic and aligning runtime-global access with the project’s Window type augmentation.

Changes:

  • Switched runtime-global accesses from globalThis to window for TerraDraw and deck.gl providers to satisfy existing Window augmentation typing.
  • Adjusted TerraDraw tool configuration typing (getProviderConfig(): unknown) and updated mode constructors to use constructor-parameter-derived option types.
  • Fixed a Google Maps polygon typing mismatch by asserting the paths type, and added a SonarCloud suppression for typescript:S7764.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Providers/Maps/Google/Shape/Polygon.ts Casts polygon paths to google.maps.LatLngLiteral[] to resolve TS mismatch.
src/Providers/Layers/deck.gl/HeatmapLayer.ts Uses window.deck instead of globalThis.deck to align with Window typings.
src/Providers/DrawingTools/TerraDraw/Tools/DrawRectangle.ts Uses window.terraDraw and constructor-parameter typing for mode options.
src/Providers/DrawingTools/TerraDraw/Tools/DrawPolyline.ts Uses window.terraDraw and constructor-parameter typing for mode options.
src/Providers/DrawingTools/TerraDraw/Tools/DrawPolygon.ts Uses window.terraDraw and constructor-parameter typing for mode options.
src/Providers/DrawingTools/TerraDraw/Tools/DrawMarker.ts Uses window.terraDraw and constructor-parameter typing for mode options.
src/Providers/DrawingTools/TerraDraw/Tools/DrawCircle.ts Uses window.terraDraw and constructor-parameter typing for mode options.
src/Providers/DrawingTools/TerraDraw/DrawingTools.ts Adds an enabled guard in dispose() and swaps TerraDraw globals to window.
src/Providers/DrawingTools/TerraDraw/Configuration/DrawPolylineConfig.ts Changes provider config return type to unknown and removes array casts.
src/Providers/DrawingTools/TerraDraw/Configuration/DrawMarkerConfig.ts Changes provider config return type to unknown and removes array casts.
src/Providers/DrawingTools/TerraDraw/Configuration/DrawFilledShapeConfig.ts Changes provider config return type to unknown and removes array casts.
src/Providers/DrawingTools/TerraDraw/Configuration/DrawConfig.ts Updates abstract getProviderConfig() signature to return unknown.
sonar-project.properties Adds project-wide Sonar ignore rule for typescript:S7764.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Providers/DrawingTools/TerraDraw/DrawingTools.ts Outdated
Comment thread src/Providers/DrawingTools/TerraDraw/DrawingTools.ts Outdated
Comment thread src/Providers/DrawingTools/TerraDraw/DrawingTools.ts
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Apr 27, 2026

Quality Gate Passed Quality Gate passed

Issues
0 New issues
16 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

@rugoncalves rugoncalves merged commit abbb239 into dev Apr 27, 2026
16 checks passed
@rugoncalves rugoncalves deleted the ROU-12774 branch April 27, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants