Hookyns / tst-reflect

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

[Question] Does this work with functions? #90

Closed lastmjs closed 10 months ago

lastmjs commented 10 months ago

Is this library supposed to be able to return the type information of functions at runtime?

I want something like this to work:

import { getType } from 'tst-reflect';

type nat32 = number;
type Vec<T> = T[];

function testRoomba<query>(param1: nat32, param2: nat32): Vec<nat32> {
    return [param1, param2];
}

console.log('logging from main file', getType(testRoomba));

It logs:

Object({"_typeParameters": Array([]), "_interface": Undefined, "_indexes": Array([]), "_isUnion": Bool(false), "_genericTypeConstraint": Undefined, "_name": String("testRoomba"), "_properties": Array([]), "_isIntersection": Bool(false), "_genericTypeDefault": Undefined, "_constructors": Array([]), "_functionSignatures": Array([]), "_methods": Array([]), "_kind": Int(14), "_typeArgs": Array([]), "_literalValue": Undefined, "_conditionalType": Undefined, "_indexedAccessType": Undefined, "_genericTypeDefinition": Undefined, "_ctorDesc": Object({}), "_baseType": Object({"typeResolver": Object({}), "resolvedType": Object({"_kind": Int(2), "_isIntersection": Bool(false), "_indexedAccessType": Undefined, "_isGenericType": Bool(false), "_literalValue": Undefined, "_genericTypeConstraint": Undefined, "_properties": Array([]), "_baseType": Undefined, "_types": Array([]), "_isUnion": Bool(false), "_constructors": Array([]), "_typeParameters": Array([]), "_methods": Array([]), "_ctorDesc": Object({}), "_interface": Undefined, "_name": String("Object"), "_fullName": String("Object"), "_functionSignatures": Array([]), "_genericTypeDefinition": Undefined, "_indexes": Array([]), "_decorators": Array([]), "_ctor": Object({}), "_typeArgs": Array([]), "_genericTypeDefault": Undefined, "_conditionalType": Undefined})}), "_decorators": Array([]), "_fullName": String("@@dynamic/18a42311e691"), "_ctor": Undefined, "_isGenericType": Bool(false), "_types": Array([])})

I don't see the type information I need for the params, return type, or type parameters.

Hookyns commented 10 months ago

Hi,

You have to use the generic argument of the getType; You can pass only primitive types or classes as runtime argument.

This will return correct type.

getType<typeof testRoomba>()

See the demo.

PS: It is possible to use getType(testRoomba) in Alpha version (discontinued) - alpha demo here - and it will be possible with the upcoming TypeGen.

lastmjs commented 10 months ago

Okay, thank you.