Skip to content

Reflection Support for Types and Other Constructs #49610

@CoryGH

Description

@CoryGH

Suggestion

The specific issue I'm running in to relates to reflection on TypeScript type constructs, specifically with:

export enum eNum {
    FOO = 0,
    BAR = 1
}
export type foo = {
    bar: string,
    baz?: number,
    qux?: eNum
};

I would like to be able to discern:

{
    name: "foo",
    members: [
        {
            key: "bar",
            optional: false,
            definition: "string"
        },
        {
            key: "baz",
            optional: true,
            definition: "number"
        },
        {
            key: "bar",
            optional: true,
            definition: "eNum"
        }
    ]
}

In a more general sense, TypeScript is sorely lacking in a lot of reflection functionality, to the point getting a similar result from an enum looks something like:

public static parseEnum(enumType: { [id: string]: number|string }): string {
    let skipNext = false;
    let dontSkipNext = false;
    let ret = '{\n\t';
    let entryCount = 0;
    for (let entry in enumType) {
        let hit = false;
        if ((!skipNext) || dontSkipNext) {
            if (isNaN(Number(entry))) {
                // @ts-ignore
                dontSkipNext = !isNaN(enumType[entry]);
                skipNext = true;
                hit = true;
            } else {
                skipNext = false;
            }
        } else {
            skipNext = false;
            dontSkipNext = false;
        }
        if (hit) {
            if (entryCount > 0) {
                ret += ',\n\t';
            }
            ret += entry.toString() + ' = ';
            if (typeof enumType[entry] === "string") {
                ret += '"' + enumType[entry].toString() + '"';
            } else {
                ret += enumType[entry].toString();
            }
            entryCount++;
        }
    }
    ret += '\n}';
    return ret;
}

It would be nice if TypeScript supported reflection with 100% coverage to the point of .NET without all the hacks to work around it and the duplicate-code/boilerplate required when reflection simply isn't possible, such as in the case of type constructs.

🔍 Search Terms

Reflection, Types

✅ Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

.NET-tier reflection.

📃 Motivating Example

See above.

💻 Use Cases

See above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions