Skip to content

Commit a42f9a8

Browse files
authored
add abitity to type an accessory context using generics (homebridge#2664)
1 parent 5ace8e3 commit a42f9a8

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/platformAccessory.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import {
1313
} from "hap-nodejs";
1414
import { PlatformName, PluginIdentifier, PluginName } from "./api";
1515

16-
export interface SerializedPlatformAccessory extends SerializedAccessory {
16+
export type UnknownContext = Record<string, unknown>;
17+
18+
export interface SerializedPlatformAccessory<T extends UnknownContext = UnknownContext> extends SerializedAccessory {
1719

1820
plugin: PluginName;
1921
platform: PlatformName;
20-
21-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22-
context: Record<string, any>;
22+
context: T;
2323

2424
}
2525

@@ -35,7 +35,8 @@ export declare interface PlatformAccessory {
3535

3636
}
3737

38-
export class PlatformAccessory extends EventEmitter {
38+
39+
export class PlatformAccessory<T extends UnknownContext = UnknownContext> extends EventEmitter {
3940

4041
// somewhat ugly way to inject custom Accessory object, while not changing the publicly exposed constructor signature
4142
private static injectedAccessory?: Accessory;
@@ -59,8 +60,7 @@ export class PlatformAccessory extends EventEmitter {
5960
/**
6061
* This is a way for Plugin developers to store custom data with their accessory
6162
*/
62-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
63-
public context: Record<string, any> = {}; // providing something to store
63+
public context: T = {} as T; // providing something to store
6464

6565
constructor(displayName: string, uuid: string, category?: Categories) { // category is only useful for external accessories
6666
super();

0 commit comments

Comments
 (0)