Hookyns / tst-reflect

Advanced TypeScript runtime reflection system
MIT License
328 stars 11 forks source link

Reflect on functions #48

Closed avin-kavish closed 1 year ago

avin-kavish commented 1 year ago

I have a use case to reflect and get the parameter information of functions. Currently, it looks like it's not possible to do. I tried

  function baz(a: string) {}

  const runtimeType = getType<typeof baz>()
  console.log(runtimeType)

and got the error,

/node_modules/tst-reflect-transformer/dist/helpers.js:81
    throw new Error("Unknown type kind");
          ^
at getTypeKind (/node_modules/tst-reflect-transformer/dist/helpers.js:81:11)
at getTypeDescription (/node_modules/tst-reflect-transformer/dist/getTypeDescription.js:227:44)
at getTypeCall (/node_modules/tst-reflect-transformer/dist/getTypeCall.js:21:71)
at processGetTypeCallExpression (/node_modules/tst-reflect-transformer/dist/processGetTypeCallExpression.js:21:46)
at mainVisitor (/node_modules/tst-reflect-transformer/dist/visitors/mainVisitor.js:26:93)
at Context._visitor (/node_modules/tst-reflect-transformer/dist/contexts/Context.js:9:35)
at visitNode (/node_modules/typescript/lib/typescript.js:89421:23)
at Object.visitEachChild (/node_modules/typescript/lib/typescript.js:89933:236)
at mainVisitor (/node_modules/tst-reflect-transformer/dist/visitors/mainVisitor.js:85:15)
at Context._visitor (/node_modules/tst-reflect-transformer/dist/contexts/Context.js:9:35)
Hookyns commented 1 year ago

Available in: tst-reflect@0.7.9, tst-reflect-transformer@0.9.12

Example

import {
    getType,
    Type
} from "tst-reflect";

function foo<T extends string = "">(x: T, y: number)
{
    return 1;
}

const x = function(a: boolean) {}
const y = (a: boolean) => a;

printFunctionInfo(getType<typeof foo>());
printFunctionInfo(getType<typeof x>());
printFunctionInfo(getType<typeof y>());
printFunctionInfo(getType(foo));

function printFunctionInfo(fnc: Type)
{
    console.log("----------------------------------------");
    console.log("function", fnc.name, fnc.fullName);

    if (fnc.function)
    {
        console.log("return type:", fnc.function.returnType.name);
        console.log("parameters:", fnc.function.getParameters().map(param => param.name + ": " + param.type.name).join(", "));
        console.log("type parameters:", fnc.function.getTypeParameters().map(param => param.name).join(", "));
    }
}

Output

----------------------------------------
function foo @@this/dev/quick-tests/index.ts:foo#85
return type: Number
parameters: x: T, y: Number
type parameters: T
----------------------------------------
function __function @@this/dev/quick-tests/index.ts:__function#93
return type: void
parameters: a: Boolean
type parameters:
----------------------------------------
function __function @@this/dev/quick-tests/index.ts:__function#94
return type: Boolean
parameters: a: Boolean
type parameters:
----------------------------------------
function foo @@dynamic/1827517a3d21
return type: unknown
parameters: param0: any, param1: any
type parameters:
Hookyns commented 1 year ago

@all-contributors add @avin-kavish for bug report.

allcontributors[bot] commented 1 year ago

@Hookyns

I've put up a pull request to add @avin-kavish! :tada: