0

I am trying to create this type reference:

Parameters<Foo['bar']>[0]

how do I achieve this using ts.createTypeReferenceNode?

ts.createTypeReferenceNode('Parameters', [
    ts.createTypeReferenceNode(`Foo['bar']`, []),
])

allows no array access.

1 Answer 1

2

According to ts-ast-viewer.com, you'd need to use ts.createIndexedAccessTypeNode and ts.createLiteralTypeNode:

        ts.createIndexedAccessTypeNode(
          ts.createTypeReferenceNode(
            ts.createIdentifier("Parameters"),
            [ts.createIndexedAccessTypeNode(
              ts.createTypeReferenceNode(
                ts.createIdentifier("Foo"),
                undefined
              ),
              ts.createLiteralTypeNode(ts.createStringLiteral("bar"))
            )]
          ),
          ts.createLiteralTypeNode(ts.createNumericLiteral("0"))
        )
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Using ts.createIndexedAccessTypeNode solved it for me. ts-ast-viewer.com is a great tip, previously I manually figured out how to generate the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.