Skip to content
New issue

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

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

Already on GitHub? # to your account

[TS] Fixing keyof type operator for ActionDict #395

Closed
Phamier opened this issue Aug 11, 2022 · 2 comments
Closed

[TS] Fixing keyof type operator for ActionDict #395

Phamier opened this issue Aug 11, 2022 · 2 comments
Labels
types Issues related to the TypeScript type definitions
Milestone

Comments

@Phamier
Copy link

Phamier commented Aug 11, 2022

Introduction

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch ohm-js@16.4.0 for the project I'm working on.

Problem

String mapping in type ActionDict breaks keyof type operator.

Example

I wanted to make a type containing actions keys of my grammar.

type MapKeys = Exclude<keyof FormulaActionDict<any>, keyof ActionDict<any>>;

Instead of returning union of string literal types keyof ActionDict<any> returns string | number. So type MapKeys == never.

Solution

Here is the diff that solved my problem:

diff --git a/node_modules/ohm-js/index.d.ts b/node_modules/ohm-js/index.d.ts
index 9265d48..db49216 100644
--- a/node_modules/ohm-js/index.d.ts
+++ b/node_modules/ohm-js/index.d.ts
@@ -273,8 +273,6 @@ declare namespace ohm {
    * An ActionDict is a dictionary of Actions indexed by rule names.
    */
   interface ActionDict<T> {
-    [index: string]: Action<T> | undefined;
-
     _iter?: (this: IterationNode, ...children: Node[]) => T;
     _nonterminal?: (this: NonterminalNode, ...children: Node[]) => T;
     _terminal?: (this: TerminalNode) => T;

This issue body was partially generated by patch-package.

@Phamier Phamier changed the title [TS] Fixing keyof type Operator for ActionDict [TS] Fixing keyof type operator for ActionDict Aug 11, 2022
@pdubroy
Copy link
Contributor

pdubroy commented Sep 15, 2022

Thanks for opening this issue. I think this makes sense now that we supported generating TypeScript type definitions for grammars. The original type definition was intended to give some basic type information for generic action dictionaries. This would be a breaking change though.

@pdubroy pdubroy added this to the Ohm v17 milestone Sep 17, 2022
@pdubroy pdubroy added the types Issues related to the TypeScript type definitions label Sep 17, 2022
@pdubroy pdubroy closed this as completed in 87db4df Nov 6, 2022
@pdubroy
Copy link
Contributor

pdubroy commented Nov 6, 2022

This is now fixed on main, and will be included in the upcoming v17 release. However, you'll have to use BaseActionDict to get the behaviour you're looking for.

import * as ohm from 'ohm-js';
import greeting, {GreetingActionDict} from './greeting.ohm-bundle';

type MapKey = Exclude<keyof GreetingActionDict<any>, keyof ohm.BaseActionDict<any>>;
const x: MapKey = 'hello';

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
types Issues related to the TypeScript type definitions
Projects
None yet
Development

No branches or pull requests

2 participants