samchon / typia

Super-fast/easy runtime validations and serializations through transformation

Home Page:https://typia.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature request] printTsName & printTsContent for Type/Interface/Enum

mattaiod opened this issue · comments

Feature request

Hello :)

That would be great to have a function like printTsName and printTsContent :

type TypeExample = string ;

printTsName<TypeExample>() // return "TypeExample"
printTsContent<TypeExample>() // returns "string"

It would be usable with all typecript things like: Type, Interface, Enum

Thank you very much and I look forward to your feedback on this request.

May I add the function under the reflect scope?

I think it would better to add this feature to the v6.3 update.

Also, the new feature should be designed to one function. Instead, you can suggest the second generic argument.

typia.reflect.metadata<[Types]>(); // current exsing
typia.reflect.name<T, EscapeOrAlias??SuggestionPlease>(); // new feature

Until the update, you can accomplish your purpose by below way:

import typia from "typia";
import { MetadataApplication } from "typia/lib/schemas/metadata/MetadataApplication";

type TypeExample = string;

const app = MetadataApplication.from(typia.reflect.metadata<[TypeExample]>());
console.log(app.metadatas[0].getName());

https://typia.io/playground/?script=JYWwDg9gTgLgBDAnmYBDOAzKERwERIqp4DcAUKJLHAN5wCyApjKgCaosCCYYANsAGMOwCADs4AX0zZcBZGgD0-AEYKAzgIAWjEKjUKQzNh1QKmLdlx78hMEaNJkyhRnAAqyRgFEAHqnC8rgC8cGowUMCiAObkZAJiYXCoPHAh5sZWfILCYgB0WDgAFIRouVCMGIECMLmGFiYAPADaHmDefgGMALoAfIUAlP3k8aJqEIG5vBBRhclgtUaWek0ADF25UcwAcv6MA0NAA

Okay, thanks !

Would it also be possible to add variables?

for example :

const variableExample = 3
printTsName<typeof variableExample>() // return "variableExample"

Yes, the reflect scope sounds perfect for this ;)

In actually, type statement in compiler API is considered as alias.

It is possible to trace the significant type name through ts.TypeNode API, but typeof statement may not.

Also, even in the Type case, if it is a little bit compliate, cannot sure of exact tracing.

npm install typia@next

Install next version, and try as you wish.

The new function's name is typia.reflect.name<T>().

import typia, { tags } from "typia";

type Something = string & tags.Format<"uuid">;
const value: number = 3;

console.log(
  typia.reflect.name<string & tags.Format<"uuid">>(),
  typia.reflect.name<number & tags.Type<"uint32">>(),
  typia.reflect.name<Something, true>(),
  typia.reflect.name<Something, false>(),
  typia.reflect.name<typeof value>(),
  typia.reflect.name<typeof value, false>(),
  typia.reflect.name<
    string &
      tags.JsonSchemaPlugin<{
        "x-typia-something": true;
      }>
  >(),
);
string & tags.Format<"uuid"> number & tags.Type<"uint32"> Something Something typeof value typeof value
    string &
      tags.JsonSchemaPlugin<{
        "x-typia-something": true;
      }>

@samchon

Thank you from the bottom of my heart The best programmer in Korea. It's perfect!

What about simplify this thing ?

const app = MetadataApplication.from(typia.reflect.metadata<[TypeExample]>());
console.log(app.metadatas[0].getName());

I suppose the best would be something like :

type Something = string & tags.Format<"uuid">;
typia.reflect.type<Something>() // return "string & tags.Format<"uuid">"
or 
typia.reflect.metadata.getType<Something>() // return "string & tags.Format<"uuid">"

The function name should not be type, because the type means metadata definition.

Also, you can do same thing with Metadata.getName() by configuring second argument as true.

typia.reflect.name<T, true>()

@samchon Okay, this is perfect.

Thanks again for your incredible work, it's very helping to bridge the gap between typescript and javascript.

Have a good day