In typescript if you have 2 complex objects and you want to use an instance of one as the other, it might just work. In the case of generic type arguments at least, prepare for potentially really difficult to read tooltip parsing. Or say you are importing a json file, the type it is importing as may be much more generic than you desired. This is especially true if you have any properties that are only allowed to be specific strings instead of any string.
Given the limited context here from the typescript package
@types/topojson-specification
type Positions = number[];
type Arc = Positions[];
interface Objects<P> { [ key: string]: GeometryObject<P>}
interface Topology<T extends Objects<GeoJSON.GeoJsonProperties> = Objects<GeoJsonProperties>>{
type:"Topology",
arcs:Arc[]
}
import * as globeData from './globedata.json';
We can see the types are quite a mental mess to think about. Using Type alias proxies can work wonders.
type TopKey = keyof Topology
type Top = {
[K in TopKey]: Topology[K]
}
Now with these defined (that we can remove later once we figure out what all is going on) we can proceed with property checks.
// type is a special case, the types match, but they must be an allowed string, so we do this workaround
var gdt:Top['type'] | undefined = globeData.type == "Topology"? globeData.type : undefined;
var gda:Top['arcs'] = globeData.arcs;
We see here that we can declare a type in the context of another type's property. This greatly slims the surface of the tooltip explaining if something is wrong on that property and what it is. Repeat the breakdown process as necessary.
No comments:
Post a Comment