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

Possibility to support "Node IndexedAccessType" #37

Open
xzilja opened this issue Mar 25, 2021 · 2 comments
Open

Possibility to support "Node IndexedAccessType" #37

xzilja opened this issue Mar 25, 2021 · 2 comments

Comments

@xzilja
Copy link

xzilja commented Mar 25, 2021

export interface ICharacter {
  id: string;
  username: string;
}

export interface ICreateCharacterArguments {
  username: ICharacter['username'];
};

Currently generating from this file fails with following error
Error: Node IndexedAccessType not supported by ts-interface-builder: ICharacter['username']

EDIT: Additional context on this type https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html

@xzilja
Copy link
Author

xzilja commented Mar 26, 2021

I'm trying to play around with this and essentially tweaked

case ts.SyntaxKind.IndexSignature:
return this._compileIndexSignatureDeclaration(node as ts.IndexSignatureDeclaration);

and added additional case at the end

case ts.SyntaxKind.IndexedAccessType:
         return this._compileIndexSignatureDeclaration(node);

But I am then getting stuck here

if (!node.type) { throw new Error(`Node ${ts.SyntaxKind[node.kind]} must have a type`); }

I am not sure where this type needs to be set / added at the moment

@dsagal
Copy link
Member

dsagal commented Mar 26, 2021

IndexedAccessType is a different kind of node than an IndexSignatureDeclaration. So your case should look like

case ts.SyntaxKind.IndexedAccessType:
   return this._compileIndexedAccessType(node as ts.IndexedAccessTypeNode);

You can learn more about that node type by finding it in the TypeScript source code: TypeScript/src/compiler/types.ts.

As for what code ts-interface-builder should actually emit in this case, that's the hard part. I think it requires a new kind of node that ts-interface-checker would have to support, maybe something like t.typeProp("ICharacter", "username"). But TypeScript is actually more flexible -- https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html -- so to support it better, it perhaps should be t.typeProp("ICharacter", t.lit("username")). And it would need to be implemented on the ts-interface-checker side.

There might be other ways of doing it -- e.g. if ICharacter["username"] is known to be string, just to emit "string". There is enough inference in TypeScript compiler that it's probably possible, at least in some cases, but probably not in general.

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

No branches or pull requests

2 participants