microsoft / microsoft/TypeScript

Expose serializers from TypeScript transformer (ts.ts)

Open
#17,516 10 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: API Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

With the introduction of the Transform API some of the logic in transformers/ts.ts needs to be exposed. (and maybe from other places...)

I'll explain by example:

@SomeDecorator()
export class MyClass {
  constructor(public myInterface: MyInterface, public myClass: MyOtherClass) {}
}

If I want to transform the code so all parameter type metadata will be on a static property of the class. (this is angular DI)

@SomeDecorator()
export class MyClass {
  constructor(public myInterface: MyInterface, public myClass: MyOtherClass) { }

  static ctorParams: [Object, MyOtherClass];
}

This operation is currently impossible.

To get the right identifier for each type I need to serialize the type with some logic so MyInterface will become Object since it's not really a value.

The native TypeScript transformer has a lot of code for handling the logic of type serialization and it's not something one will want to copy, its huge.

Even if someone decided to replicate the logic, there's not access to the EmitResolver which is the "type checker" used by the native transformer to serialize the types.

TransformationContext.getEmitResolver() is @internal

The workaround is to actually run another Program process to get access to a TypeChecker.
It's not even possible to get a hold of the current running Program instance since TransformerFactory does not get access to it.

Is it possible to expose the serializatoin logic in the TypeScript transformer? I know it's complex, recursing and state dependant (scope) but it seems like not being able to serialize a type to create a literal is something basic

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/compiler/transformers/ts.ts and the Transform API, then inspect TransformationContext.getEmitResolver() and the EmitResolver usage described in the issue. Determine the public API boundary needed to reuse type serialization for custom transformers, and verify that parameter types such as MyInterface serialize to Object without requiring a second Program.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.