Skip to content

Commit

Permalink
add abitity to type an accessory context using generics (homebridge#2664
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mgcrea authored Sep 11, 2020
1 parent 5ace8e3 commit a42f9a8
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import {
} from "hap-nodejs";
import { PlatformName, PluginIdentifier, PluginName } from "./api";

export interface SerializedPlatformAccessory extends SerializedAccessory {
export type UnknownContext = Record<string, unknown>;

export interface SerializedPlatformAccessory<T extends UnknownContext = UnknownContext> extends SerializedAccessory {

plugin: PluginName;
platform: PlatformName;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
context: Record<string, any>;
context: T;

}

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

}

export class PlatformAccessory extends EventEmitter {

export class PlatformAccessory<T extends UnknownContext = UnknownContext> extends EventEmitter {

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

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

0 comments on commit a42f9a8

Please sign in to comment.