Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed constants #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

ferm10n
Copy link
Contributor

@ferm10n ferm10n commented Jun 2, 2022

What

  • convert json consts to js, and add type defs for them

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

John Sanders added 2 commits June 2, 2022 16:06
- this is useful for dependent projects that want type safety
- there's no way to import a json as a const type (where `subscribe` is stricter than `number`) so thats why the convert to js (see microsoft/TypeScript#32063)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant