Ts-morph: How to get 'questionToken' from getValueDeclarationOrThrow()?

Created on 18 Aug 2019  路  3Comments  路  Source: dsherret/ts-morph

I wrote the following code:

public getTypeDetail(node : Type)
{
    let callSignatures = node.getCallSignatures(); // 'node' is Type, 'getCallSignatures()' returns Signature[]
    let params = signature.getParameters().map(x => {
        return {
            isOptional : x.getValueDeclarationOrThrow().getQuestionTokenNode() !== undefined // getQuestionTokenNode does not exist.
        }
    }
}

How to get QuestionToken information for each parameter of getCallSignatures() ?

I can find it in the final result but no method found!

Screenshot_2

question

All 3 comments

The getValueDeclarationOrThrow() returns a Node type. In this case you should be able to just assert it as a ParameterDeclaration.

Also, there are more ways for a parameter declaration to be optional, so you'll probably want to use the isOptional() helper method, which will handle all the scenarios for you (ex. it could be a rest parameter or have an initializer):

(x.getValueDeclarationOrThrow() as ParameterDeclaration).isOptional()

@dsherret

Is isOptional() helper method available for PropertyDecleration and PropertySignature ?

image

image

@HamedFathi no, there's only one way for a property to be optional. There only exists a #hasQuestionToken() method. If an #isOptional() method existed it would just do return this.hasQuestionToken().

Was this page helpful?
0 / 5 - 0 ratings