Skip to content

Commit

Permalink
feat(labels-layer): 支持使用范型约束方法的传参和返回值
Browse files Browse the repository at this point in the history
add,remove,getAllOverlays
  • Loading branch information
xyy94813 committed Jun 23, 2024
1 parent b431da4 commit 6ff13c1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
15 changes: 8 additions & 7 deletions types/layers/LabelsLayer.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { VectorOverlay } from '../overlays/VectorOverlay';
import type { LabelMarker } from '../overlays/LabelMarker';

import type { BaseLayer, CommonLayerOptions } from './BaseLayer';
Expand All @@ -16,7 +15,9 @@ export type LabelsLayerOption = CommonLayerOptions & {
*
* @example new AMap.LabelsLayer(opts: LabelsLayerOptions)
*/
export declare class LabelsLayer extends BaseLayer {
export declare class LabelsLayer<
TOverlayType extends LabelMarker = LabelMarker
> extends BaseLayer {
constructor(options?: LabelsLayerOption);

/**
Expand All @@ -41,18 +42,18 @@ export declare class LabelsLayer extends BaseLayer {
setAllowCollision(allowCollision: boolean): void;
/**
* 将 labelMarker 添加到标注层上
* @param {LabelMarker | LabelMarker[]} labelMarkers
* @param {TOverlayType | TOverlayType[]} labelMarkers
*/
add(labelMarkers: LabelMarker | LabelMarker[]): void;
add(labelMarkers: TOverlayType | TOverlayType[]): void;
/**
* 将 labelMarker 从标注层上移除
* @param {LabelMarker | LabelMarker[]} labelMarkers
* @param {TOverlayType | TOverlayType[]} labelMarkers
*/
remove(labelMarkers: LabelMarker | LabelMarker[]): void;
remove(labelMarkers: TOverlayType | TOverlayType[]): void;
/**
* 获取标注层内的所有标注对象
*/
getAllOverlays(): VectorOverlay[] | undefined;
getAllOverlays(): TOverlayType[] | undefined;
/**
* 清空 VectorLayer
*/
Expand Down
22 changes: 19 additions & 3 deletions types/layers/LabelsLayer.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/no-confusing-void-expression */
/* eslint-disable @typescript-eslint/no-invalid-void-type */
import { expectAssignable, expectType } from 'tsd';
import { expectAssignable, expectError, expectType } from 'tsd';
import LabelMarker from '../overlays/LabelMarker';
import { VectorOverlay } from '../overlays/VectorOverlay';
import { BaseLayer } from './BaseLayer';
import {
LabelsLayerOption,
Expand Down Expand Up @@ -41,6 +40,23 @@ expectType<void>(layer.remove(labelMarker));
expectType<void>(layer.remove([]));
expectType<void>(layer.remove([labelMarker]));

expectType<VectorOverlay[] | undefined>(layer.getAllOverlays());
expectType<LabelMarker[] | undefined>(layer.getAllOverlays());

expectType<void>(layer.clear());


declare class LabelMarker2 extends LabelMarker {
a: string
}

// 验证自定义范型
const layer2 = new LabelsLayer<LabelMarker2>();
expectType<void>(layer2.add([]));
expectType<void>(layer.remove([]));

expectError<void>(layer2.add(labelMarker));
expectError<void>(layer2.add([labelMarker]));
expectError<void>(layer2.remove(labelMarker));
expectError<void>(layer2.remove([labelMarker]));

expectType<LabelMarker2[]>(layer2.getAllOverlays()!);

0 comments on commit 6ff13c1

Please sign in to comment.