I'm trying to conditionally return either KML or GeoJSON (string and object types respectively), and I'm wondering how to assign a function like that through an interface on an Object method.
Say the object is
const api = {
getGeometry(url, format) {
return fetch(`${url}?format=${format}`)
}
}
With an interface like this:
interface GeometryRequest {
(url: string, format: 'kml'): Promise<string>
(url: string, format: 'geojson'): Promise<GeoJSON.FeatureCollection<any>>
}
So I can get that to work as a plain function:
const geometryRequest: GeometryRequest = (url, format) => {
return fetch(`${url}?format=${format}`)
}
I'm just wondering how I can assign a type to an object method in a similar way.
Here are a couple of methods I tried:
getGeometry<GeometryRequest>
getGeometry: GeometryRequest
Neither are proper syntax.
I've also added a simpler version of the same problem on TypeScript Playground. In the playground output's type is: const output: string | number. But it should be able to tell the type from the overloaded functions in the interface somehow.
Any help on this syntax problem would be appreciated! :)