st-one-io / node-open-protocol

This node is an implementation of the Atlas Copco's Open Protocol. This node was created by Smart-Tech as part of the ST-One project.
GNU General Public License v3.0
39 stars 38 forks source link

Typed constants #34

Open ferm10n opened 2 years ago

ferm10n commented 2 years ago

What

Why

This is useful for dependent projects that want type safety. For example:

import midGroups from 'node-open-protocol/typings/src/midGroups';
type MidGroups = typeof midGroups;

// filter out the mid groups that don't have `subscribe`
type Subscribable = {
  [ midGroup in keyof MidGroups ]: MidGroups[midGroup] extends { subscribe: number } ? MidGroups[midGroup] : never;
}

// only valid subscription mid group names would be allowed
const cmd: keyof Subscribable = 'psetSelected';

// only valid subscription mid ids would be allowed
const subscribeMid: Subscribable[keyof Subscribable] = midGroups.psetSelected.subscribe;

Rationale