ts-morph icon indicating copy to clipboard operation
ts-morph copied to clipboard

Get properties from ObjectType using an EcmaScript symbol instead of a string

Open Melchyore opened this issue 1 month ago • 3 comments

Is your feature request related to a problem? Please describe.

Hi.

I have a symbol called HEADERS, defined as follows:

export const HEADERS = Symbol.for('__headers')

I'm using this symbol to augment the type of a method:

ok<T, Instance extends this>(
  body: T,
  etag?: boolean
): {
  __response: T
  __status: GetStatus<Instance, 200>
  [HEADERS]: ReturnType<Instance['append']>
}

Now, when I read the AST of this method, I have access to the ObjectType, which is

{
  __response: T
  __status: GetStatus<Instance, 200>
  [HEADERS]: ReturnType<Instance['append']>
}

I want to get the [HEADERS] property using my symbol

type.getProperty(HEADERS)

But that's not possible, cuz the getProperty method accepts only a string as parameter or a function. For the latter, I did this and it worked

getProperty((property) =>
  property.getName().startsWith('__@HEADERS')
)

... but I wonder if it's possible to use only the symbol.

Describe the solution you'd like

The getProperty method should accept a symbol as parameter.

Describe alternatives you've considered

getProperty((property) =>
  property.getName().startsWith('__@HEADERS')
)

Is it possible to achieve this, please?

Melchyore avatar May 30 '24 14:05 Melchyore