Effect CommunityEC
Effect Community10mo ago
18 replies
spaethnl

How to Override Dependency Types in TypeScript?

Is there any straight-forward way of to override the types in a dependency, or bridge the incompatibility I have here?
I am using turfjs, which uses the types from @types/geojson . That package defines type Position = number[], but I have modeled it with schema for my domain with more strict types:

export const Latitude = pipe(S.Number, S.between(-180, 180), S.brand('Latitude'))
export type Latitude = typeof Latitude.Type

export const Longitude = pipe(S.Number, S.between(-90, 90),S.brand('Longitude'))
export type Longitude = typeof Latitude.Type

// in meters, from sea level
export const Altitude = pipe(S.Number,S.brand('Altitude'))
export type Altitude = typeof Altitude.Type

export const Position2d = S.Tuple(Longitude, Latitude).pipe(S.brand('2d-Position'))
export type Position2d = typeof Position2d.Type
export const Position3d = S.Tuple(Longitude, Latitude, Altitude).pipe(S.brand('3d-Position'))
export type Position3d = typeof Position3d.Type

export const Position = S.Union(Position2d, Position3d)
export type Position = typeof Position.Type

//.... other geojson types ...


However, now I can't use @turf/turf utilities because:

declare const myGeometry: Geometry
export const x = turf.getCoords(myGeometry)

// Types of property 'coordinates' are incompatible.
//  Type 'Position2d | Position3d' is not assignable to type 'Position'.
 //   Type 'Position2d' is not assignable to type 'number[]'. 

declare const myPosition: Position2d
export const y = turf.getCoords(myPosition)    
// The type 'readonly [number & Brand<"Longitude">, number & Brand<"Latitude">]'
// is 'readonly' and cannot be assigned to the mutable type 'any[]'
Was this page helpful?