Skip to content

[Question] How to extract argument from generic type? #1616

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

Open
adi-doorloop opened this issue Feb 22, 2025 · 2 comments
Open

[Question] How to extract argument from generic type? #1616

adi-doorloop opened this issue Feb 22, 2025 · 2 comments

Comments

@adi-doorloop
Copy link

adi-doorloop commented Feb 22, 2025

It's either a brain fart or an actual issue.

export type MutableStep1Dto = MutableDto<Step1Dto, "userType" | "propertyTypes">;

I want to extract the second argument of MutableDto to count the union members. I tried walking the nodes but it just doesn't yield the expected types as seen in the AST viewer, see https://ts-ast-viewer.com/#code/FAUwHgDg9gTgLgAjgTwiBBZArnAhgIwBsQBlOECANgBE4oEBeTHA42qAHjIproBoEAIiwBnEDAAqqEIIQAfIRBhQ08ZFLQjBAPgDcQA.

@markandrus
Copy link

import { Project } from 'ts-morph'

const project = new Project()

const sourceFile = project.createSourceFile('test.ts', `\
export type MutableStep1Dto = MutableDto<Step1Dto, "userType" | "propertyTypes">;
`)

const tyNode = sourceFile
  .getTypeAliases()[0]
  .getTypeNodeOrThrow()
  .getTypeArguments()[1]

console.log(tyNode.getText())

// "userType" | "propertyTypes"

@adi-doorloop
Copy link
Author

adi-doorloop commented Mar 6, 2025

Thanks. Creating a source file seems a bit wild for this purpose.

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