我查看了leaflet.js
定义文件,在那里我发现了一个奇怪的事情:Marker<P = any>
,在那里我无法弄清P = any
的用途。我的意思是为什么这不是P: any
?
类实现:
export class Marker<P = any> extends Layer {
constructor(latlng: LatLngExpression, options?: MarkerOptions);
toGeoJSON(): geojson.Feature<geojson.Point, P>;
getLatLng(): LatLng;
setLatLng(latlng: LatLngExpression): this;
setZIndexOffset(offset: number): this;
setIcon(icon: Icon | DivIcon): this;
setOpacity(opacity: number): this;
getElement(): HTMLElement | undefined;
// Properties
options: MarkerOptions;
dragging?: Handler;
feature?: geojson.Feature<geojson.Point, P>;
}
完整定义文件: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/leaflet/index.d.ts
答案 0 :(得分:3)
Marker
是泛型类型,类型参数名为P
。通常,您必须为泛型类型指定泛型参数。但是,如果泛型类型的定义为类型参数(P = any
)提供了缺省值,则可以在省略显式类型参数的情况下使用该类型:
let x: Marker // valid because there is a default of any for P will be the same as Marker<any>
let xy: Marker<number> // valid because Marker is generic